Monday, November 30, 2015

Static(Class) Methods & Instance(Non-static) methods

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.

class A
{
    public static void  X()
  {
          Y();
  }
  public static void Y()
  {
        System.out.println("parent");
  }
}

public class B extends A
{
        public static void Y()
        {
                System.out.println("Child");
        }
        public static void main(String[] args) {
                new A().Y();
           }
}

Output:
parent


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.

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.

2) Can we override static methods in java? What is method hiding in Java? What is the output of below program?
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Parent{
 public static void print(){
  System.out.println("I am Parent");
 }
}

class Child extends Parent{
 public static void print(){
  System.out.println("I am Child");
 }
}

public class MainClass {
 public static void main(String args[]) {
  Parent parent = new Parent();
  parent.print();
   
  parent = new Child();
  parent.print();
   
  Child child = new Child();
  child.print();
 }
}

Output : 
I am Parent 
I am Parent 
I am Child

Explanation:

No. Static methods cannot be overridden. 

Static methods are not polymorphic and don’t take part in runtime or dynamic polymorphism and the decision as to which method will be called is resolved at compile-time based on the type alone.

We can declare static methods with the same signature in the subclass, but it is not considered as overriding because there won’t be any run-time or dynamic polymorphism.
If a derived class defines a static method with the same signature as a static method in base class, the method in the derived class hides the method in the base class.

 
even if caller writes like,  
Parent parent = new Parent();  
parent.print(); 

Compiler at compile time will change above line to Parent.print() because static methods need to be called in a static way and is not associated with any instance.

Parent parent = new Child();  
parent.print();

Above line would have printed "I am Child" if static methods are polymorphic.
So internally what it does is, Compiler checks whether print() method is static, if yes, then it replace the instance to instance type.
the parent object is of type Parent, so it replaces it to,
Parent.print();

No comments:

Post a Comment