my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static members and inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static members and inheritance" Watch "static members and inheritance" New topic
Author

static members and inheritance

Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
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.


M.Sc. in CS, OCPJP6 93%
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Nov 04, 2009
Posts: 2066

Polymorphism is only for objects, not for classes. No Polymorphism => No virtual method invocations. static members are bounded at compile time(Early binding).


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Mar 07, 2010
Posts: 438

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

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jun 20, 2010
Posts: 45
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

Joined: Mar 07, 2010
Posts: 438

@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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static members and inheritance
 
Similar Threads
Inheritance
it's long but i can't help it.
Static behaviour
Static Method and Inheritence.
Why doesn't the subclass variable hide it?