• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Inheritance made me Mad today

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following program :-



Oooopppssss!!!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. you cant override static method, since it is not bound to an Object. but it can be re-defined[in subclass also give static modifier]

2. class attribute can not be inherit in subclass. i.e, super class name reference variable is different from subclass name variable

hth
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:1. you cant override static method, since it is not bound to an Object. but it can be re-defined[in subclass also give static modifier]

2. class attribute can not be inherit in subclass. i.e, super class name reference variable is different from subclass name variable

hth



IMO , they are inherited , consider the following code :-



Prints Animal !!! evidence that static members are inherited.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not inheritance. A static member belongs to the class where it is defined.

You can use the class name to reference it. You can use an instance of the class to reference it. In the later case, the compiler replaces the instance's name by the class's name.

The compiler knows that Dog extends Animal. If it cannot find a definition in Dog for the static variable "name", it substitutes Dog for Animal. It will go up the hierarchy tree until it finds a definition or concludes that it is not defined.

As Seetharaman mentioned, static members can be redefined. In which case, the compiler finds the definition right away.

If you look carefully, you will see that static variables and static method behave the same way.



Here is the output of the code above:
Animal
Dog
Animal method()
Dog method()

Thank you. Take care.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Related to Overriding & Overloading,
1) A compilation error occurs if an instance method overrides a static method.
2) A compilation error occurs if a static method hides an instance method.
3) It's possible for a static variable to hide an instance variable.
4) It's also permissible for an instance variable to hide a static variable.

For static methods, the compiler uses the declared type of reference. That's what we mean when we say a static method doesn't have run-time polymorphism, Because instance methods and class methods have this important different in behaviors, we use different terms - "overriding" for instance methods, and "hiding" for class methods.

When we say, you can't override a static method, what that means is even if you write code that looks like it's overriding a static method - it won't behave like an overriden method(no polymorphism)
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abimaran......Thats what i was asking !!!
I got it now....

But i also observed the same behaviur and wrote as rules.....But do you know the logic behind above rules ???
 
reply
    Bookmark Topic Watch Topic
  • New Topic