| Author |
Abstract Classes / Methods
|
colin shuker
Ranch Hand
Joined: Apr 11, 2005
Posts: 712
|
|
Hi, I have a class AAA(abstract), with an abstract method method1(). Class BBB(abstract) extends AAA, but does not contain method1(). Classes CCC1,CCC2(both concrete) extends BBB, and implements method1(). This is shown here This actually works, but as you can see method1() does not appear in BBB. I guess the point is method1() is being implemented by in a subclass of AAA, its just not a direct subclass, but a sub-subclass. Is this a bad thing to do, implementing behaviours for methods declared at the top of a hierachy, at the bottom? Also, I think this only works because BBB is abstract, and if it were not, then it must implement method1(). Any thoughts? Thanks
|
 |
greg buela
Ranch Hand
Joined: Sep 04, 2007
Posts: 71
|
|
You are right about why it works. I see nothing wrong with doing it, it's a matter of your model desing, you may have a case where such desing is the right thing.
|
SCJP 1.5
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
Consider an exampleMost animals make a sound so it is reasonable to have the makeSound method declared in the Animal class. A mammal doesn't make a specific sound, so there is no need to override the makeSound method in the Mammal class. Dogs and pigs have specific sounds so you implement the makeSound method in those classes.
|
Joanne
|
 |
colin shuker
Ranch Hand
Joined: Apr 11, 2005
Posts: 712
|
|
|
Thanks good example, that clears it up well.
|
 |
 |
|
|
subject: Abstract Classes / Methods
|
|
|