This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding methods" Watch "Overriding methods" New topic
Author

Overriding methods

Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
This quote is from Tony Morris QnA

A subclass is dependant on its superclass, and this dependancy is unidirectional. A superclass cannot have a dependancy on a subclass.How can a developer design a superclass that knows about ALL of its subclasses? Deriving from a class provides you with the same functionality as the superclass. How does the superclass know about the subclass, since it is added at a later date (due to the unidirectional dependancy)? Perhaps what it is in the subclass actually belongs in the superclass? Perhaps you need to use the Template Method Design Pattern? This is clearly speculation and a more concrete solution can only be arrived at with some more thought on your specific problem.


Consider this code


o/p:40

In the Test class we are creating an object of the Derived class.The Derived class constructor is called.This in turn calls Base class constructor.method addValue() is overriden in the Base class.When this method is called from the Base class I think addValue() from the Base class should be the one which should be called since Base class knows only about its methods and variables and nothing about methods or variables in the derived class.Why is the method addValue() form the Derived class called?Does it not contradict concept of inheritance.What I believe is that subclass is more specialized and superclass knows nothing about the specific behavoir of its subclass.please explain.


Shweta Dhaneshwar.<br />SCJP 1.4 90%
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
If you add the following statement System.out.println(this); in Base class constructor then you doubt will become clear.The call to the method addValue(); is equivalent to this.addValue();


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
John Wolf
Ranch Hand

Joined: Feb 18, 2002
Posts: 64
Shweta,

This a great example and I learned something from it. What's happening here is , as per the Template Method Pattern, the Base.class (superclass) is letting the Derived.class (subclass) redefine the certain steps in the algorithm. In other words, the Derived.class (subclass) is providing the implementation of the addValue() method.

If you remove the addValue() method from the Derived.class the superclass will use its own implementation of addValue() as expected.

The superclass does not need to know about the subclass, the dependency, like you said, is unidirectional. Its like parent teaching the kid how to do somthing, and kid mostly follows all the steps later, but few of the steps the kid does its own way. Only at runtime, the superclass lets the subclass follow steps "in its own way." Not the best analogy, but it helps me.

Thanks for the Post!
Sumit


SCJP 1.2, SCJP 1.4, SCJA, SCWCD 1.4<br /> <br />------------------------------<br />
 
jQuery in Action, 2nd edition
 
subject: Overriding methods
 
Similar Threads
Inheritance question
Question about Parent call childe method
why the output is so?
polymorphism Problem (tricky)
This is a good one about inheritance