Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding

 
ganga prabhu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone plz clear this doubt



When the overriding method is commented ...how it is printing super as output

my friend told that whenever a class is inherited and method in it is not overridden same code is inlined in derived class..

on the basis of it when variable is looked it should print sub only but how super is displaying

Anybody explain little bit clearly
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true to say that methods are inherit in derived classes, not that "same code is inlined in dervived classes".

Here the method getDesc() is called from Super, and variable d in getDesc() refer to variable d in Super.

Note that variables are not overriden, but only shadowed. (It can be a good practice to not use same variable names in sub-classes as in super classes).

Change the name of variable d in Sub, maybe it can help to understand.
 
Ishan Bhatt
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you comment the overridden method it means that you are not overridding.
So, obviously it will call the method in the super class. And for the super class "d" means "super".
Hence the o/p.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Super s = new Sub();System.out.println(s.getDesc());


s is have refernce to Super that d as same as s.d ( reference to Super Class )
 
ganga prabhu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hi somkiat

According to ur words

call to method1 should refer super class method1 but it is refering sub class method1(still we have base class references for that object)

why this happening
 
Kris Krason
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code will not compile.

You can't call static method from non-static one.
If you had a call to method1() from main(), then it should print the superclass string.
 
ganga prabhu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can't call static method from non-static one.


We can call static method from non-static method
but we cannot call non-static method from static method(without having object of the class)
 
Kris Krason
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, you are right.
It wouldn't make sense the other way
 
ganga prabhu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok anybody clear my 2nd post doubt in this topic
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
overriding is resolved at Runtime.
Since your Runtime object is over1(subclass), and also the getD method is overriden in over1 class, so it calls over1's (subclass) getD method. Further the getD method calls method1 which is shadowed in over1(subclass) so method1 of over1 is called which prints: "Sub Class"

Hope I am not wrong
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic