• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Mock Question

 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The answer is
Derived.amethod()
99
Derived.amethod().
I did not understand why did the base constructor call the method amethod of Derived class instead of Base class.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this article on polymorphism, and let us know if that clears it up.
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


--- indenting repaired, Bu.
[ September 16, 2007: Message edited by: Burkhard Hassel ]
 
amit manchanda
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private methods dont get override hence base class method was called
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Yours,
Bu
 
Chandrasekhar Mangipudi
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic