• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try to explain best I can.
I have created a bunch of classes(10). Each class has many different methods but they all have the same method named:
getMyColor()
So, in another class I need the constructor to have one of the possible 10 classes I created. I built the constructor like this
class newClass(Object x) {
x.getMyColor(); // error
I figure the above error is because there is no getMyColor() method defined for the Object class.
Should I take all my 10 classes and have them be subclasses under another class? Or is there another possiblity that I don't know about?
I apologize if this makes no sense, any help welcome.
Bill

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use either an abstract class or an interface. I would use an interface, with one method in it called getMyColor(). Then, make all your 10 classes implement that interface. As long as they all define that method (which you already stated, they do) you then define newClass like this:

Note that instead of passing an Object into newClass, you pass in the type of interface that all your classes have implemented. This is interpreted as "any object type that implements the interface".
 
Bill Norton
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So let me get this straight, I should use a abstract class or interface: if the function is same name but different code, but if the function is same name, same code, should I use a super-class?
That said I now need to go read up on the differences between an abstract class and interface. Other than interface can be implemented, for multiple inheritance... i think :-)

Originally posted by ryan burgdorfer:
[B]You need to use either an abstract class or an interface. I would use an interface, with one method in it called getMyColor(). Then, make all your 10 classes implement that interface. As long as they all define that method (which you already stated, they do) you then define newClass like this:

Note that instead of passing an Object into newClass, you pass in the type of interface that all your classes have implemented. This is interpreted as "any object type that implements the interface".[/B]


 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the method getMyColor() is the same in each of your 10 classes, which it sounds like is true, it would be best to define an interface with the method defined there. As long as your classes implement the interface, I *think* you don't need to provide the method getMyColor() within them. They will implicitly have it since they implement the interface. Could someone verify/correct me on this?
If the method definitions are different at all between your 10 classes, an abstract super-class would be best, with the method getMyColor() declared abstract with no definition. Then you are forced to define the method in each class extending the super-class. I have described the interface way above...the way you would do it using an abstract super-class is very similar:
Declare your abstract super-class, with one abstract method in it named getMyColor() (with no definition). All your other classes already define that method, so the only change needed for them is to make them extend SuperClass. Then, in your method newClass, just pass in the name of your abstract super-class.
Very little difference from using an interface, as you can see. I think using an interface would still be your best bet though, at least for future re-usability or adding onto your code, if nothing else.
Anyone else have any comments on this? I'm definitely no expert on the matter by any means, and would appreciate clarification/verification myself
------------------
  • Ryan Burgdorfer
  • Java Acolyte


  • [This message has been edited by ryan burgdorfer (edited April 28, 2001).]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ryan & Bill,
If the code for getMyColor() is the same for all 10 classes, the usual refactoring would be to extract the method to a new superclass, make it at least protected in the superclass, then make all the 10 classes extend the new superclass.
If the 10 classes are not really related and inheritance doesn't make sense, use the interface approach. Each class that implements the interface has to either provide a full implementation of the interface (i.e. the class has an implementation of getMyColor()) or declare itself as abstract (full implementation will be done in a subclass).
Junilu Lacar
in Worthington, OH

[This message has been edited by JUNILU LACAR (edited April 28, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu is correct. Ryan, you have it exactly backwards. All method declarations in Interfaces are implicitely abstract and MUST NOT provide a method body. However Ryan is correct in his example of how to use the interface (or abstract class) as a parameter.
From JLS 9.4 Abstract Method Declarations


Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
Every method declaration in the body of an interface is implicitly public.
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.


 
Bill Norton
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK this helps alot.
So if the classes are related with each other I could use a superclass, as long as the method is the same, or override the method in the subclass if needed?

but if the classes are not related than it is better to use an abstact interface with the method
getMyColor();
which says every class the implements me(abstract class) must define this method?
Am I close?
Bill
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the METHODS that have the same name across the classes do not use the exact same code it is better to use the interface.
Other than that I think that you have it.
 
Bill Norton
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply, sorry to bother with one last question(u hope :-)
If the classes are related why would it not be better to have a superclass and then have each subclass override it?
Thanks
Bill
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just put the coommon code in the super class and DON'T override it. Then all the sub-classes inherit the method and can use it like their own. Saves your poor fingers some typing.
Oh - just re-read the question.
1 If you are always going to override a method why waste the only extends that you have, when you can implement an interface instead and get the same benefits. What if you NEED to extend something else?
2. Implementing an interface provides a less "coupled" or "brittle" relationship between the two. It is further removed from the implementation details and is therefore more purely object oriented.

[This message has been edited by Cindy Glass (edited April 29, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic