Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance made me Mad today

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following program :-



Oooopppssss!!!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Yves Caron
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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 ???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic