Professional Documents
Culture Documents
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.
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.}
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.
public public
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?
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.
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.}
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.
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?
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.
No question of overriding private methods. They are not at all inherited to sub class.
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. }
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
Output:child
Output:child