Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Justify the output of the code (inheritance/overriding problem)

 
pranav thakker
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

Can anyone justify the output of following code ?



According to myself output should be as follow :-

Parent's method2()
Child's method1()

Because, we are calling method2 with reference of child class.

But, the actual output is :-


Parent's method2()
Parent's method1()

Please, explain it.

Thanking you all,

Pranav Thakker
 
Lukas Stephienn
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output

Parent's method2()
Parent's method1()

is absolutely correct.

method1 cannot be invoked in polymorphic way because you do not override it. As long as it is marked private it cannot be inherited.
If you would change it to public your output will be as you thought.
 
lavanya sankuappan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private method,method1() in parent class is not overridden by child class.it is another method with the same name
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If u change the method1() in parents class like:

then the output would be
Parents method2()
Child method1()


Thanks
Balaji.S
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But, the actual output is :-
Parent's method2()
Parent's method1()


It's a Child object and a Child declaration, so how is it possible that the output above has "Parent's method1()"?!!
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search JavaRanch and you can find more posts about this.
Here is one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic