This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static members and inheritance

 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following two examples -

Example 1:

Example 2:

All of the static member resolution is done by the compiler. Thus, there is no scope of runtime polymorphism with static member of a class.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unmesh Chowdhury wrote:
All of the static member resolution is done by the compiler. Thus, there is no scope of runtime polymorphism with static member of a class.


Sounds about right to me.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism is only for objects, not for classes. No Polymorphism => No virtual method invocations. static members are bounded at compile time(Early binding).
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dear Abimaran Kugathasan. How do you can get object without any class? A class is the template of its objects and an object is constructed as it is defined in it's class.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unmesh Chowdhury wrote:Hi Dear Abimaran Kugathasan. How do you can get object without any class? A class is the template of its objects and an object is constructed as it is defined in it's class.


Due to the combination of the class hierarchy and the fact that a class can implement many different interfaces, a single object in Java can be treated as many different types. And In your case, the ultimate goal of the class is to create object.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply..
the object reference types decide which static methods or the static variables to be used
and another point I want to clear is
inherited methods are NOT copied (let the experts comment more on this) they are same as those in superclass and
they are not actually copied into subclass
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Kharkar wrote:Simply..
the object reference types decide which static methods or the static variables to be used

I would disagree as objects have nothing to do with static methods or variables. What matters is the class, in this example the class type of the variable, not the object it refers to (even if the variable refers to null). Regardless, one shouldn't try to dereference static variables or methods with variables but rather with classes to avoid this confusion.
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Abimaran Kugathasan, not only in my case, in every case, the ultimate goal of the class is to create object. It is true that in Java an object can be of different types according to the class hierarchy and interfaces. In the case of interface, an object will be an interface type, if it's class implements that interface. In fact, if a class does not implement a particular interface then the objects of that class will not be of that particular interface type. Thus, a class is the blueprint of an object and an object is just a run time instance of a class.

Thus, there is no scope of runtime polymorphism with static member of a class


The above statement means that, it will be true when objects are created from this class, that is, it will be a characteristics of this class objects. And my thought was that, this implicit meaning is cleared to every folks like you.
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@pete
I wanted to say Reference type of the variable (wanted to say what you have said )
thank you for correcting English mistake
have a nice time to all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic