This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

polymorphism Problem (tricky)  RSS feed

 
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
 
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...

 
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.
 
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.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!