aspose file tools*
The moose likes Beginning Java and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Bill Norton
Greenhorn

Joined: Mar 29, 2001
Posts: 27
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

ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
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".

<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Bill Norton
Greenhorn

Joined: Mar 29, 2001
Posts: 27
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

Joined: Jan 24, 2001
Posts: 219
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).]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

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).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Bill Norton
Greenhorn

Joined: Mar 29, 2001
Posts: 27
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Mar 29, 2001
Posts: 27
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance
 
Similar Threads
constructor concepts...
Constructor
Doubts about my classes
Why Is a No-Arg Constructor Required Here?
ActionListener as a parameter for a constructor