Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymorphism Problem (tricky)

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you have already put the sys.out so i think you understand how the flow of the code is going on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic