Forums Register Login

Static Method , Inheritance & Polymorphism

+Pie Number of slices to send: Send
I am not too sure if this is the right forum. Some conceptual questions on object oriented features of java.

Are Static methods inherited?
I think the answer is yes.

Does Static methods support polymorphism?
I think the answer is No.

What does Java gain by providing inheritance of static methods when you cannot have polymorphism?

I have not been able to come up with a good answer to this.
+Pie Number of slices to send: Send
One advantage is that since the non-private static methods are inherited by subclasses, you don't have to qualify the static methods when you call them in the subclass unless you hide the implementation.
+Pie Number of slices to send: Send
class A{

public static void staticMethodX()
{
System.out.println("I am in Class A staticMehodX");
}

public void methodY()
{
System.out.println("I am in Class A MehodY \n");
}
}



class B extends A{

public static void staticMethodX()
{
A.staticMethodX();
System.out.println("I am in Class B staticMehodX");
}

public void methodY()
{
System.out.println("I am in Class B MehodY \n");
}
}



class C extends A{

public void methodY()
{
System.out.println("I am in Class C MehodY \n");
}
}


class ABCTester {
public static void main(String[] args) {

A a = new A();
a.staticMethodX();
a.methodY();

B b = new B();
b.staticMethodX();
b.methodY();

A a1 = new B();
a1.staticMethodX();
a1.methodY();

C c = new C();
c.staticMethodX();
c.methodY();
C.staticMethodX();

}
}


Output::

I am in Class A staticMehodX
I am in Class A MehodY

I am in Class A staticMehodX
I am in Class B staticMehodX
I am in Class B MehodY

I am in Class A staticMehodX
I am in Class B MehodY

I am in Class A staticMehodX
I am in Class C MehodY

I am in Class A staticMehodX


So the following codes:
a.staticMethodX();// Call through instance
b.staticMethodX();// call over the method in B class which hide the parent class static method.
a1.staticMethodX();// Compiler resolving at compile time and referrring to the Parent class static method.
c.staticMethodX();//Calling through instance of C to make sure that object of
C.staticMethodX();//Calling as if it were static class method of C


Points Agreed:
1) As pointed out, inheriting makes it sure that we need not give the class name
2) Not a good practice to call it through instantiated objects for static Methods. It should be called using the class name like A.staticMethodX();

Points to be pondered upon:
1) Now there are 2 ways in which I can invoke staticMethodX() :: Following point 2 above, which is the agreed good practice.
A.staticMethodX();
C.staticMethodX();

Though the actual definition is in Class A, I can still invoke it through C.

What is the difference between this 2 invokation?
+Pie Number of slices to send: Send
Refer the URL. I think the explanation provided should help you understand the difference.

Remember static methods are resolved at compile time and instance methods at runtime.
+Pie Number of slices to send: Send
Hi Vinay,
The explanation is not too clear. It just says that Dog.bark() can be referred to as Dalmatian.bark() - when if we used Dog.bark(), it will get the Dog version while if we use the Dalmatian.bark(), it will get the Dalmatian version.

I understand the instance variable and static thing at compile and runtime.

But I don't understand the difference between:
A.staticMethodX();
C.staticMethodX();

How will the JVM resolve , let's say when I have a program which calls C.staticMethodX(). C extends A and has not overridden/hidden staticMethodX of A?

[ August 11, 2006: Message edited by: Ingudam Manoranjan ]
[ August 11, 2006: Message edited by: Ingudam Manoranjan ]
+Pie Number of slices to send: Send
 

Originally posted by Ingudam Manoranjan:

How will the JVM resolve , let's say when I have a program which calls C.staticMethodX(). C extends A and has not overridden/hidden staticMethodX of A?



It won't, but the compiler will - that's the whole point. The generated byte code will already contain a call to A.staticMethodX().
+Pie Number of slices to send: Send
Thanks Ilja. Since it is resolved at compile time, it is preety clear now.
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 910 times.
Similar Threads
static methods
are static methods inherited
Can static methods be inherited?
Compile time polymorphism
A doubt on overriding
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:41:41.