• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

polymorphism Problem (tricky)

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic