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

Understanding inheritance

 
Bhaskar Mukhejee
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have started java recently. Got a snippet of a code. can anybody please explain this inheritance?
I have got 2 classes Test1 and Test2. Test1 inherits Test2.



The output is :
20
Executing method of Test1

but when I am replacing Test1 with Test2 The output is :
40
Executing method of Test1

It is showing accessing the instance variable of Test2 class but method of Test1 class why?

Please help. Thank you.

Regards,
Bhaskar
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhaskar Mukhejee wrote:

but when I am replacing Test1 with Test2 The output is :
40
Executing method of Test1
Is this what you have done.

Regards,
Bhaskar


So here t refers to Test2 But at runtime the instance is created of Test1 so it overrides the math method of Test1.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhaskar Mukhejee wrote:It is showing accessing the instance variable of Test2 class but method of Test1 class why?


It's because variables aren't overridden, only methods are.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This FAQ might help you.
 
Bhaskar Mukhejee
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for this help.

Regards,
Bhaskar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic