• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok anybody clear my 2nd post doubt in this topic
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic