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?



3. Can we override static methods of super class in sub class?
  • NO.Its 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. }



  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. }

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. }

  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. }


method overriding example program interview question


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 ?


 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. }

  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. System.out.println("Sub class add method");

  7. }

  8. }

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

  7. }

  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. System.out.println("Sub class add method");

  7. }

  8. }

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 can not change from unchecked to checked 

 You might Like:

 

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for Top 10 Interview Programs and questions on method overriding in java

  1. We can change the return type of overridden method in sub class. But, subclass must return subclass of the overridden method. Please find example for the same:

    package com.steg.practice;

    import java.io.IOException;

    public class SuperClass {

    public Number add() {
    System.out.println("Adding anything");

    return 10;
    }
    }

    package com.steg.practice;

    import java.io.FileNotFoundException;

    public class SubClass extends SuperClass {

    @Override
    public Integer add() {
    System.out.println("Adding in Sub");

    return 8;
    }

    public static void main(String ...aa) {

    SubClass ob = new SubClass();
    ob.add();
    }
    }

    ReplyDelete
    Replies
    1. in your example you are returning sub class object : same return type,
      super and sub classes are same types not different.

      Delete

Select Menu