Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Doubt in overrididng

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can any one tell me why thge output of below code is Hi pal!!!, I am BaseClass isn't it example of overriding and polymerphism


Thanks in advance
 
Greenhorn
Posts: 27
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the concept of overriding, but here at runtime it is referring to the base class reference so it was calling base class method
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very simple
Static methods are not inherited by child class and hence cannot be overriden.
Only instance methods are inherited by child class and therefore be overriden.
think about why static methods are not inherited.
Thanks
Deepak
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the parent has a static method, the Child cannot see it, so if no overriding rules are violated, the code would work fine. Here, if either static is over-ridden to non-static OR non-static is overridden to static, the compiler will complain. Else, both the static methods are correct in their own scope. And whenever the static method is called, during compilation only it is decided that it will be called using the reference method and not the object method it is referring to.
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks I got it . But didnt get why static methods are not inherited.Please explain the reason,Deepak.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Divya

static methods are class methods. Hence they are never inherited.

Murali...
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi divya !

which static methods/variables and instance variables to be called are solely decided at complie time and not run time. For them the compiler only sees the "Class Type" of the reference variable used to invoke them. Dynamic method invocation is only for instance methods.

You donot override the static method/variables, but you simply hide them in your sub-classes. This is called shadowing of methods and variables. But even though you are hiding the static methods you still have to follow all the overriding rules.

And more importantly static method cannot be shadowed as non static method in SUb-Class and vice-versa...

I hope this explaination helps !!
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code shows static methods are not inherited because they are static


Error:
The method iAmStaticMethod() is undefined for the type Child

Which static method to invoke is decided at compile time.
Inheritance is only restricted to instance methods and not static methods.
Static methods are @ class level and not at object level.
Now since static methods are not inherited they cannot be overriden.
Read Second Chapter in K&B [Static members]
Thanks,
Deepak
 
    Bookmark Topic Watch Topic
  • New Topic