You are on page 1of 9

Method Overriding

1.What is method overriding in java?

 Defining multiple methods with same name and same signature in super class and sub
class known as method overriding. 
 Method overriding is type of polymorphism in java which is one of the main object
oriented feature.
 Redefined the super class method in sub class is known as method overriding.
 Method overriding allows sub class to provide specific implementation that is already
defined in super class.
 Sub class functionality replaces the super class method functionality
(implementation).
2. Can we override private methods in java?
 No. It’s not possible to override private methods because private methods in super
class will not be inherited to sub class.  

3. Can we override static methods of super class in sub class? 

 No it’s not possible to override static methods because static means class level so
static methods not involve in inheritance.

 4. Can we change the return type of overridden method in sub class?
 No. Return type must be same in super class and sub class.

1. package MethodOverridingExamplePrograms;
2. public class Super{
3.  
4. void add(){
5.  System.out.println("Super class add method");
6. }
7.
8. }

1. package MethodOverridingInterviewPrograms;
2. public class Sub extends Super{
3.   
4. int add(){    //Compiler Error: The return type is incompatible with Super.add()
5.  
6. System.out.println("Sub class add method");
7. return 0; 
8.
9. }
10.
11.}

5. Can we change accessibility modifier in sub class overridden method? 

 Yes we can change accessibility modifier in sub class overridden method but should
increase the accessibility if we decrease compiler will throw an error message.

Super class method  Subclass method

Protected protected, public

public      public

default     default , public

6. What happens if we try to decrease accessibility from super class to sub


class?
 Compile time error will come.

1. package MethodOverridingExamplePrograms;
2. public class Super{
3.  
4. public void add(){
5.  System.out.println("Super class add method");
6. }
7.
8. }

1. package MethodOverridingInterviewPrograms;
2. public class Sub extends Super{
3.   
4. void add(){ //Compiler Error: Cannot reduce the visibility of the inherited
method
5. from Super
6.  
7. System.out.println("Sub class add method");
8.
9. }
10.
11.}
7. Can we override a super class method without throws clause to with throws
clause in the sub class?

 Yes if super class method throws unchecked exceptions.


 No need if super class method throws checked exceptions. But it is recommended to
add in sub class method in order to maintain exception messages.

 8. What are the rules we need to follow in overriding if super class method
throws exception?

  Exception handling in method overriding

 9. What happens if we not follow these rules if super class method throws
some exception.

  Compile time error will come.

1. package MethodOverridingExamplePrograms;
2. public class Super{
3.  
4. public void add(){
5.  System.out.println("Super class add method");
6. }
7.
8. }

1. package MethodOverridingInterviewPrograms;
2. public class Sub extends Super{
3.   
4. void add() throws Exception{ //Compiler Error: Exception Exception is not
compatible with
5. throws clause in Super.add()
6.
7. System.out.println("Sub class add method");
8.
9. }
10.
11.}

1. package MethodOverridingExamplePrograms;
2. public class Super{
3.  
4. public void add(){
5.  System.out.println("Super class add method");
6. }
7.
8. }

1. package MethodOverridingInterviewPrograms;
2. public class Sub extends Super{
3.   
4. void add() throws NullPointerException{ // this method throws unchecked
exception so no
5. isuues
6.
7. System.out.println("Sub class add method");
8.
9. }
10.
11.}

10. Can we change an exception of a method with throws clause from


unchecked to checked while overriding it? 
  No. As mentioned above already
 If super class method throws exceptions in sub class if you want to mention
throws then use same class or its sub class exception.
 So we cannot change from unchecked to checked 

12. What are the rules to be followed while overriding a method?

13. There are 5 main rules you should kept in mind while overriding a method. They are,

14. a) Name of the method must be same as that of super class method.

15. b) Return type of overridden method must be compatible with the method being
overridden. i.e. if a method has primitive type as it’s return type then it must be
overridden with primitive type only and if a method has derived type as it’s return type
then it must be overridden with same type or it’s sub class types.

16. c) You must not reduce the visibility of a method while overriding.

17. d) You must not change parameter list of a method while overriding.

18. e) You cannot increase the scope of exceptions while overriding a method with throws
clause.

13. Can we change the return type of overriding method from Number type to Integer
type?

Yes. You can change as Integer is a sub class of Number type.

14. How do you refer super class version of overridden method in the sub class?

Using super keyword, we can refer super class version of overridden method in the sub
class.

15. Can we override private methods?

No question of overriding private methods. They are not at all inherited to sub class.

16. Is it possible to override non-static methods as static?

No. You can’t override non-static methods as static.

ExceptionHandling with MethodOverriding


There are many rules if we talk about method overriding with exception handling. The
Rules are as follows:
o If the superclass method does not declare an exception

o If the superclass method does not declare an exception, subclass


overridden method cannot declare the checked exception but it can
declare unchecked exception.
o If the superclass method declares an exception

o If the superclass method declares an exception, subclass overridden


method can declare same, subclass exception or no exception but cannot
declare parent exception.

If the superclass method does not declare an exception


1) Rule: If the superclass method does not declare an exception, subclass overridden method
cannot declare the checked exception.

1. import java.io.*;  
2. class Parent{  
3.   void msg(){System.out.println("parent");}  
4. }  
5.   
6. class TestExceptionChild extends Parent{  
7.   void msg()throws IOException{  
8.     System.out.println("TestExceptionChild");  
9.   }  
10.   public static void main(String args[]){  
11.    Parent p=new TestExceptionChild();  
12.    p.msg();  
13.   }  
14. }  

Output:Compile Time Error

2) Rule: If the superclass method does not declare an exception, subclass overridden method
cannot declare the checked exception but can declare unchecked exception.

1. import java.io.*;  
2. class Parent{  
3.   void msg(){System.out.println("parent");}  
4. }  
5.   
6. class TestExceptionChild1 extends Parent{  
7.   void msg()throws ArithmeticException{  
8.     System.out.println("child");  
9.   }  
10.   public static void main(String args[]){  
11.    Parent p=new TestExceptionChild1();  
12.    p.msg();  
13.   }  
14. }  

Output:child

If the superclass method declares an exception


1) Rule: If the superclass method declares an exception, subclass overridden method can declare
same, subclass exception or no exception but cannot declare parent exception.

Example in case subclass overridden method declares


parent exception
1. import java.io.*;  
2. class Parent{  
3.   void msg()throws ArithmeticException{System.out.println("parent");}  
4. }  
5.   
6. class TestExceptionChild2 extends Parent{  
7.   void msg()throws Exception{System.out.println("child");}  
8.   
9.   public static void main(String args[]){  
10.    Parent p=new TestExceptionChild2();  
11.    try{  
12.    p.msg();  
13.    }catch(Exception e){}  
14.   }  
15. }  

Output:Compile Time Error


Example in case subclass overridden method declares same
exception
1. import java.io.*;  
2. class Parent{  
3.   void msg()throws Exception{System.out.println("parent");}  
4. }  
5.   
6. class TestExceptionChild3 extends Parent{  
7.   void msg()throws Exception{System.out.println("child");}  
8.   
9.   public static void main(String args[]){  
10.    Parent p=new TestExceptionChild3();  
11.    try{  
12.    p.msg();  
13.    }catch(Exception e){}  
14.   }  
15. }  

Output:child

Example in case subclass overridden method declares


subclass exception
1. import java.io.*;  
2. class Parent{  
3.   void msg()throws Exception{System.out.println("parent");}  
4. }  
5.   
6. class TestExceptionChild4 extends Parent{  
7.   void msg()throws ArithmeticException{System.out.println("child");}  
8.   
9.   public static void main(String args[]){  
10.    Parent p=new TestExceptionChild4();  
11.    try{  
12.    p.msg();  
13.    }catch(Exception e){}  
14.   }  
15. }  
Output:child

Example in case subclass overridden method declares no


exception
1. import java.io.*;  
2. class Parent{  
3.   void msg()throws Exception{System.out.println("parent");}  
4. }  
5.   
6. class TestExceptionChild5 extends Parent{  
7.   void msg(){System.out.println("child");}  
8.   
9.   public static void main(String args[]){  
10.    Parent p=new TestExceptionChild5();  
11.    try{  
12.    p.msg();  
13.    }catch(Exception e){}  
14.   }  
15. }  

Output:child

You might also like