2

For the below code:

public class NewClass { 
    public static class superclass { 
        static void print() 
        { 
            System.out.println("print in superclass."); 
        } 
    } 
    public static class subclass extends superclass { 
        static void print() 
        { 
            System.out.println("print in subclass."); 
        } 
    } 

    public static void main(String[] args) 
    { 
        superclass A = new superclass(); 
        superclass B = new subclass(); 
        A.print(); 
        B.print(); 
    } 
}

Here B.print() prints --> "print in superclass.", But I expected it to print "print in subclass." as 'B' is object of subclass.

But, if I change static void print() to void print(), then it prints "print in subclass.` correctly.

Can someone help understand this?

  • 2
    Method overriding exists only for instance methods. static methods cannot be overridden. – Eran Dec 04 '19 at 06:55
  • 2
    Dynamic binding is not applicable to static methods. – ernest_k Dec 04 '19 at 06:55
  • Static methods are always belonging to its class where it is defined so you are not calling the inherited method. – Michael Kemmerzell Dec 04 '19 at 06:56
  • @mkemmerz, but `superclass B = new subclass();` will create object of subclass, right? – Garima Agarwal Dec 04 '19 at 07:00
  • @GarimaAgarwal It does! You are not calling an instance method though. It is confusing because you breka the java naming convention. B.print() is not calling the object B you declared rather it calls the static method of B. – Michael Kemmerzell Dec 04 '19 at 07:01
  • Possible duplicate https://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods https://stackoverflow.com/questions/31432068/static-method-in-inheritance – i.bondarenko Dec 04 '19 at 07:04

4 Answers4

1

Static methods are not part of the inheritance tree. They exist only tied to their classes and neither their subclasses or objects. To reduce compile errors Java allows you to call them as if they belonged to subclasses or objects but they don't.

For improved readability, you should follow common Java naming conventions: Classnames are CamelCased starting with an upper case letter, variables and members are named in camelCase starting with a lower case letter.

Nicktar
  • 5,548
  • 1
  • 28
  • 43
1

As you initialize object for superclass hence it return superclass method see the below code

public class NewClass { 
public static class superclass { 
    static void print() 
    { 
        System.out.println("print in superclass."); 
    } 
} 
public static class subclass extends superclass { 
    static void print() 
    { 
        System.out.println("print in subclass."); 
    } 
} 

public static void main(String[] args) 
{ 
    superclass A = new superclass(); 
    subclass B = new subclass(); 
    A.print(); 
    B.print(); 
} 

}

sam
  • 7
  • 3
1

We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism.

If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class.

/* Java program to show that if static method is redefined by 
   a derived class, then it is not overriding. */

// Superclass 
class Base { 

    // Static method in base class which will be hidden in subclass  
    public static void display() { 
        System.out.println("Static or class method from Base"); 
    } 

     // Non-static method which will be overridden in derived class  
     public void print()  { 
         System.out.println("Non-static or Instance method from Base"); 
    } 
} 

// Subclass 
class Derived extends Base { 

    // This method hides display() in Base  
    public static void display() { 
         System.out.println("Static or class method from Derived"); 
    } 

    // This method overrides print() in Base  
    public void print() { 
         System.out.println("Non-static or Instance method from Derived"); 
   } 
} 

// Driver class 
public class Test { 
    public static void main(String args[ ])  { 
       Base obj1 = new Derived(); 

       // As per overriding rules this should call to class Derive's static  
       // overridden method. Since static method can not be overridden, it  
       // calls Base's display()  
       obj1.display();   

       // Here overriding works and Derive's print() is called  
       obj1.print();      
    } 
}   

Output:

Static or class method from Base
Non-static or Instance method from Derived

Following are some important points for method overriding and static methods in Java.

1) For class (or static) methods, the method according to the type of reference is called, not according to the object being referred, which means method call is decided at compile time.

2) For instance (or non-static) methods, the method is called according to the type of object being referred, not according to the type of reference, which means method calls is decided at run time.

3) An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.

4) In a subclass (or Derived Class), we can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods — they are new methods, unique to the subclass.

Source

Oliver
  • 1,218
  • 1
  • 17
  • 21
0

Static method call is decided at compile time by its static declared type: the container (variable) type.

Non-static method is decided at runtime by its actual type: the class of the object created with 'new'.

The ability to dynamically choose the target method by their actual type at runtime (not their container type) is called polymorphism.

Raku Zeta
  • 21
  • 4