Top Method Overriding Interview Questions And answers In Java


1.what is method overriding?

  • If subclass has the same method name and same method arguments as declared in the super class is known as method overriding in java.

Advantage of method overriding :

  • using method overriding we can implement the super class method according to the specification of sub class.

The Following code shows the method overriding.


package com.practice;

public class Parent {
public void print(){
System.out.println("parent class print method");
}

}
package com.practice;

public class Child extends Parent {

public void print(){
System.out.println("im child class print method");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Child c = new Child();
c.print();

}

}

output


im child class print method

in above example we are modifying the super class method in sub class . This is known as method overriding.


2.What are the rules of method overriding in java?

The Following Rules Need to be followed while method overriding in java.

  • method signatures must be matched.that means Name of the method and method arguments must be same as declared in super class.

  • return type must be same till java 1.4 version, form java 1.5 version onwards Co-variant return types allowed.

3.Can we override private methods?

  • No,we cannot override the private methods. Because private methods are not visible to the subclasses.so we cannot override the private methods.

4.Can we override the super class non-abstract method as abstract method in subclass?

  • Yes, we can override the super class non-abstract method as abstract method in subclass To avoid the super class method implementation availability to the subclass.

Check the following code for above explanation.

package com.practice;

public class Parent {
public void print(){
System.out.println("parent class print method");
}

}
package com.practice;

public abstract class Child extends Parent {

public abstract void print();


}


5.Can we override the final methods?

  • We cannot override the final methods.which means we cannot override the super class final method as non-final method in sub class.

Lets see following code.


package com.practice;

public class Parent {
public final void print(){
System.out.println("parent class print method");
}

}




In above code if we try to override the final method we will compile time error as “Cannot override the final method from Parent”.

6.Can we override static methods?

  • No,we cannot override the static methods.

7.Can we override superclass static method as subclass non-static method?

  • No,we cannot override the super class static method as a subclass non-static method. Have a look at following code.


package com.practice;

public class Parent {
public static void print(){
System.out.println("parent class print method");
}

}


In above code you will get compile time error as “This instance method cannot override the static method from Parent.”. Similarly we cannot override the superclass non static method as subclass static method.

8.Can we override the super class protected method as public method in subclass?

  • Yes.while overriding we can increase the scope of the modifiers.Lets see following code



public class Parent {
protected void print(){
System.out.println("parent class print method");
}

}
package com.practice;

public class Child extends Parent {

public void print() {
System.out.println("child method");
}
public static void main(String[] args){
Child c = new Child();
c.print();

}


}

output


child method

9.What is Co-variant Return Type In java?

  • This is one of rule on method overriding. From java 1.5 version we have this feature which means the subclass method return type may be different from super class method return type,but subclass method return type should be a subtype of superclass method return type.For example we have super class method return type as Object.then in subclass we can have return types as a String.

Lets see following Code.


package com.practice;

public class Parent {
public Object print(){

return null;
}

}
package com.practice;

public class Child extends Parent {

public String print() {

return "abc";
}
public static void main(String[] args){
Child c = new Child();
c.print();

}


}

10.What is the exception hierarchy while method overriding in java?

  • If any super class method throws checked exception then compulsory subclass method should throw same checked exception or its subclass exception.Lets see following code.


package com.practice;

public class Parent {
public void print()throws Exception{

System.out.println("hello parent");
}

}
package com.practice;

import java.io.IOException;

public class Child extends Parent {

public void print()throws IOException {
System.out.println("hello child");
}
public static void main(String[] args) throws Exception{
Child c = new Child();
c.print();

}


}

output


hello child

No comments:

Post a Comment