1
public class B extends A{

    public static void main(String[] args) {
    new B().privateMethod();//no error -output B-privateMethod.Sounds like overriding
    new B().staticMethod(); //no error -output B-StaticMethod.Sounds like overriding
    }

    private void privateMethod() {
        System.out.println("B-privateMethod.");
    }
    static void staticMethod() {
        System.out.println("B-StaticMethod.");
    }
}


class A{
    private void privateMethod() {
        System.out.println("A-privateMethod.");
    }
    static void staticMethod() {
        System.out.println("A-StaticMethod.");
    }
}

On R&D I found in case of privateMethod()- since this method was not available on object of child class so child class's and parent class's privateMethod() are separate method and they have no relationship so this is not overriding. but in case of staticMethod()- parent class's method was available on object of child class ,and when we define this in child class, object of child class start pointing to child class method.this looks like method overriding but not,since static method does not override.

how does static method handle by java developement kit?

  • static members are not overridden, they are hidden. new B().staticMethod(); => this is a wrong approach. you'll lead others to believe it is an instance method. The correct way to call this method is: B.staticMethod(); – Stultuske Apr 20 '15 at 06:21

3 Answers3

2
new B().privateMethod();

this is not overriding, since B doesn't see A's privateMethod().

new B().staticMethod();

this is not overriding, calling a static method via an instance is allowed, though it can be confusing. It is exactly the same as calling it via the class name - B.staticMethod(). If a super class A of B has a static method visible from B, you can call that method from B (and it doesn't matter if you write B.staticMethod() or A.staticMethod() or new B().staticMethod().

If later you define a static method of the same name in B, that method hides the method of the same name in A, so calling B.staticMethod() or new B().staticMethod() now invokes B's static method. However, calling A.staticMethod() will still invoke A's static method.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • could you please put your comment on difference between method hidding and overriding except calling of static methods are determined at compile time by called with JVM instructions invokestatic, whereas polymorphism is achieved at runtime with invokevirtual, for more clearity. –  Apr 20 '15 at 06:52
  • @dubey-theHarcourtians It sounds like you are asking me to copy content from another answer into my answer. This doesn't sound right to me. – Eran Apr 20 '15 at 07:24
  • I am saying-could u plz go trough more clear on it except hidding determined at compile time on the basis of reference,since when no reference means it is calling by object compiler assume the class name of the object you will create at runtime, as a reference in case of static. –  Apr 20 '15 at 07:32
1

Polymorphism is not for static methods. Static methods are called with JVM instructions invokestatic, whereas polymorphism is achieved with invokevirtual. The calls to static methods are determined at compile time, and polymorphic methods are dynamically dispatched at runtime.

You can easily tweak your code so that A.staticMethod() is called, by just assigning new B() to a variable of type A.

public static void main(String[] args) {
    new B().privateMethod();
    A b = new B(); // change here.
    b.staticMethod(); // A.staticMethod() is called here. 
}
neo
  • 26
  • 2
0

Never speak about static and override in the same sentence.

The whole concept of overridable methods is to dynamically bind at runtime which method is to be executed. Consider this:

class A { void print() { out.println("A"); }
class B extends A { void print() { out.println("B"); }

A obj = new B();
obj.print();

Although the variable obj is of type A, it still prints out "B".

Static methods on the other hand are bound at compile time. This means the compiler uses the type of the variable (or the expression) to determine what method to execute:

class A { static void print() { out.println("A"); }
class B extends A { static void print() { out.println("B"); }

A obj = new B();
obj.print();

This now yields "A". Unfortunately the Java language allows to call static methods on variables or expressions. This is not recommended! Better call static methods on the type itself:

A.print();
B.print();

In the first example - obj.print(); - the compiler automatically translates the statement into A.print(). The actual object does not count. In fact you could write the following:

A obj = null;
obj.print();

Or:

((A) null).print();

That still prints "A".

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66