• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Static Method , Inheritance & Polymorphism

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ingudam Manoranjan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ingudam Manoranjan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Ingudam Manoranjan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ilja. Since it is resolved at compile time, it is preety clear now.
 
What's brown and sticky? ... a stick. Or a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic