Results 1 to 5 of 5
  1. #1
    namohammad is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Post How to eliminate method duplicacy ?

    There are three classes X, Y and Z as shown below, where a method "show" in all three classes has same logic.

    class X extends BaseClass1{
    show(){
    ----
    ----
    ----
    }
    }

    class Y extends BaseClass2{
    show(){
    ----
    ----
    ----
    }
    }

    class Z extends BaseClass3{
    show(){
    ----
    ----
    ----
    }
    }

    Now, I like to remove,

    show(){
    ----
    ----
    ----
    }

    from Classes X, Y and Z. Would like to reuse the "show" method from common location(i.e either class or interface or from any source).

    As the above three classes have already being extended to some BaseClass1/2/3 classes, I lost the idea of inheritance. As, Java
    does not support multiple inheritance. I have no idea how to implement the same.

    Any idea how to solve this issue w.r.to coding? Can any one help me on this ?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    A static method in another class?

    Edit or another class that "BaseClass1/2/3" extend, but the inheritance mecahnism is not really about "code reuse", if you find yourself using it for only that purpose you are doing something wrong.

  3. #3
    namohammad is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Post

    I agree, inheritance is not for reusability, but it is also for inheriting the features of parent class.

    So I can assume your idea is like this, please correct me if i'm wrong.

    Class Temp {

    static show {
    -----
    -----
    -----
    }

    }

    Class BaseClass1 extends Temp{

    }

    Class BaseClass2 extends Temp{

    }

    Class BaseClass3 extends Temp{

    }

    And finally,

    Class X extends BaseClass1 {

    }

    so on.....

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    If you do it like that (it's perfectly valid) you don't even need to make the show() method static.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    namohammad is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  2. Replies: 6
    Last Post: 04-13-2009, 05:19 AM
  3. Replies: 2
    Last Post: 12-30-2008, 03:10 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. Avoid/Eliminate Duplicate Code
    By kicker in forum New To Java
    Replies: 6
    Last Post: 09-11-2008, 05:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •