jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Mock Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Mock Question" Watch "Doubt in Mock Question" New topic

Doubt in Mock Question

neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37

The answer is
I did not understand why did the base constructor call the method amethod of Derived class instead of Base class.

Neha Verma
marc weber

Joined: Aug 31, 2004
Posts: 11343

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
neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37
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.
neha verma
Ranch Hand

Joined: Jun 17, 2007
Posts: 37
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.

--- indenting repaired, Bu.
[ September 16, 2007: Message edited by: Burkhard Hassel ]
amit manchanda

Joined: Jul 28, 2007
Posts: 3
private methods dont get override hence base class method was called
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
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.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Chandrasekhar Mangipudi
Ranch Hand

Joined: Jan 29, 2006
Posts: 118

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 ]

Thanks & Regards,
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt in Mock Question
It's not a secret anymore!