aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes polymorphism Problem  (tricky) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "polymorphism Problem  (tricky)" Watch "polymorphism Problem  (tricky)" New topic
Author

polymorphism Problem (tricky)

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

TestCafe by sahir shah


What will it print now?
output:30

i think output should be 20




will call the Derived class constructor which will in turn call Base class constructor.
now addValue method call as in BAse class Constructor would be called
then,addValue method of Base class would be called as static methods are not overriden and in case of static methods reference type determines which method to call value=10
then addValue method call in Derived class constructor would be called therefore value=10+10=20


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Derived object is created, then, Derived() is called and then base class constructor is implicitly called since super() is the first statement of each constructor....

In Base(), addValue() of Base class is called, value is incremented to 10...

After Base() constructor initialization, then Derived(), addValue() of Derived class is called, value is incremented to 30...

Note : static value is shared by Derived class from Base class due to inheritance...


SCJP 6 [SCJP - Old is Gold]
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:
then,addValue method of Base class would be called as static methods are not overriden and in case of static methods reference type determines which method to call value=10
then addValue method call in Derived class constructor would be called therefore value=10+10=20


Those are different stuffs. When you are creating the Derived class object, the compiler doesn't care about, to what reference type variable you are going to assign this object. Assigning to a super type, doesn't make any impact on the creation of the object. Only after creating the object, you are going to assign it.

And, make the method as instance methods, and predict the output. It should be 40.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Then,what about this question



output:40


here Derived class addVaue method is called twice,Base class addValue is not callled even once

why ???
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:
here Derived class addVaue method is called twice,Base class addValue is not callled even once

why ???

That's because of VMI and the actual object is Derived But, a kind advice : Don't invoke overridden methods in Constructors.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

In case of overridden method object type determines which method will call.

and object is of derived not a base so addValue of derived class is called


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

And you have already put the sys.out so i think you understand how the flow of the code is going on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polymorphism Problem (tricky)
 
Similar Threads
Overloading wih static methods
Object Overloading question
Pls help me out..
Invoking methods from constructors
constructors