Friday, 16 October 2015

Overriding Method

Overriding Method: 

If a method in the subclass has the same and same type signature as that of the method in super class, This is known as method overriding in Java.

  • It is used to achieve run time polymorphism.
  • It is used to provide the own specific implementation of a inherited method without even modifying the parent class code.
Different ways to perform method Overriding :
  • By method name : method name should be same as of super class method 
  • Number of parameter : method must have same number of parameters.
  • Access Modifier : method must not have more restrictive access modifier. 
          (More restrictive) private   ⟹   default   ⟹    protected   ⟹    public  (Less restrictive)

  • Return type : method overriding is possible by changing the return type, but only Covariant return type are allowed.
  • Exception Handling in Overriding  : check here in details
     
Case 1:  By Method Name 


package com.tutorial4u;

class Animal{
       public void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       @Override
       public void display() {
             System.out.println("I am a cat.");
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  //Output : From Super Class
             Cat cat = new Cat();
             cat.display(); //Output : From Sub Class
             Animal animal1 = new Cat();

             animal1.display();// Output from sub class
       }

}

Output : I am a animal.
              I am a cat.
              I am a cat.

Case 2: Access Modifier 

(A)
package com.tutorial4u;

class Animal{
       protected void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       public void display() {
             System.out.println("I am a cat.");
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  //Output : From Super Class
             Cat cat = new Cat();
             cat.display(); //Output : From Sub Class
       }

}

OutputI am a animal.
         I am a cat. 

(B)

package com.tutorial4u;

class Animal{
       protected void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       //Compilation Error, Cannot reduce the visibility of the inherited method from Animal

       //here visibility must be protected or public but not private or default.
       private void display() {
             System.out.println("I am a cat.");
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  
             Cat cat = new Cat();
             cat.display(); 
       }
}


Output : Compilation Error, Cannot reduce the visibility of the inherited method from Animal

Case 3: Number of parameter

package com.tutorial4u;

class Animal{
       public void display() {
             System.out.println("I am a animal.");
       }
}
class Cat extends Animal {
       public void display(int i) {
             System.out.println(i);
       }
        
}
public class OverridingExample {
       public static void main(String[] args) {
             Animal animal = new Animal();
             animal.display();  //Output : From Super Class
             Cat cat = new Cat();
             cat.display(); //Output : From super class
             cat.display(10);
       }
}

Output :
I am a animal.
I am a animal.
10

Note :  You must not change the arguments of method in subclass. If you change the number of arguments or types of arguments of overrided method in the subclass, then method will be overloaded not overrided.

Case 4:  Return Type 
You can only return same type or a subclass of the parents return type , it is known as covariant return type. 

Example : 
package com.tutorial4u;

class A {
}

class B extends A {
}

class Super {
       public A m() {
             System.out.println("Super class method");
             return new A();
       }
}

class Children extends Super {
       public B m() {
             System.out.println("Children class method");
             return new B();
       }
}

public class OverridingExample {
       public static void main(String args[]) {
             Super sup = new Super();
             sup.m();
             Children child = new Children();
             child.m();
       }
}

Output :  Super class method
Children class method

Ques 1 : Can we override static method ?
Answer :  No, But we can declare static method with same signature in sub class. It is not consider overriding as there won't be any run time polymorphism.
Here, Method in the derived class hides the method in the base class.

Example 1: 
package com.tutorial4u;

class A {

       public static void m1() {

             System.out.println("method A");

       }

}

class B extends A {

       public static void m1() {

             System.out.println("method B");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

       }

} 


Output : 
method A

Note :  If non-static method is used in above program, output would be "method B" method.

Example 2: 

package com.tutorial4u;

class A {

       public static void m1() {

             System.out.println("method a1");

       }

       public void m2() {

             System.out.println("method a2");

       }

}

class B extends A {

       //Compilation Error: This instance method cannot override the static method from A

       public void m1() {

             System.out.println("method b1");

       }

       //Compilation Error: This static method cannot hide the instance method from A

       public static void m2() {

             System.out.println("method b2");

       }

}

public class OverridingExample {

       public static void main(String args[]) {

             A a = new B();

             a.m1();

             a.m2();

       }

}

Output : 

method a1

method a2


Important Pints : 
  • private method can't be overridden in java because private method can not inherited in subclass.
  • final method can't be overridden in java because final method can not inherited in subclass.
  • static method can't be overridden in java .
  • Main() method can't be overridden in java because main() method have static method and static method can't be override.
  • Overriding method must not have more restrictive access modifier.
  • Only instance method can be override.

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...