See this article on polymorphism, and let us know if that clears it up.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
posted 11 years ago
Hi Marc, What I am able to understand is - As object of Derived class is used to invoke the Base class constructor and further upon amethod(), therefore Derived class overridden amethod is called. In case the amethod is static then the method is not overridden and so Base class amethod will be invoked. Please correct me if I am wrong. Thanks Marc.
posted 11 years ago
Hi, Here is another question-
The output is: Parent's method2() Parent's method1() Now in this example Parent s method1 is called not Child 's method1 as compared to previous example. I am confused.
In this example the child class shouldnt even know about the method declared in its super class because its declared private, so its not overriding it hence no polymorphism apply here. Try changing the access modifier to public of method1() in Parent and notice the difference.
private methods are not inherited and do not show polymorphy. The public method2 is not overridden in the Child class, so it is invoked in the parent object. It calls method1 in parent and since it is not (cannot be) overridden, "Parent's method1()" will be printed.
By the way, it would be nice if you used a propper indenting. I repaired it for you this time.
1)Base b = new Derived(); Which calls the spuer class constructor first. Super class constructor executes the method of derived class. Because overridden methods of subclass is exected when [super class ref = sub class object.].Next statement is b.i. We cant override the values of super class. so It excutes the super class value 99.Last Statement is b.amethod() which is overridden, so derived class method is executed.
2) Method1 is private method which is not overriden and method2 also not overridden.So Method2 is exected and calls its class method .
Chandrasekhar Mangipudi SCJP 1.4. Preparing scjp 1.5 (In the Process) [ September 16, 2007: Message edited by: Chandrasekhar Mangipudi ]