The Best Technical Blog Ever

twitterrss

Method Overriding in Java

Declaring the same method in subclass (child) as declared in super class (parent class) is known as method overriding. The subclass method also has the same name, number and type of parameters, and return type as the method that it overrides. Instance methods can be overridden only if they are inherited by the subclass.


The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the super class(parent class). That means we can implement super class method in subclass based on its requirements. It is used for runtime polymorphism. Binding of overridden methods happen at runtime which is known as dynamic binding. In this tutorial we will see method overriding with examples.


Example:
method overriding


Runtime Polymorphism or Dynamic Method Dispatch:

Dynamic method dispatch is a mechanism where an overridden method call is resolved at runtime. When an overridden method is called by a reference, java determines which method to execute based on the type of object it refer to in run time. Dynamic method dispatch is a technique which enables us to assign the base class reference to a child class object. When Parent class reference variable refers to Child class object, it is known as Upcasting.


method overriding


Example:
method overriding


method overriding


Program Explanation:

In dynamic method dispatch, the object can call the overriding methods of sub class and all the non-overridden methods of base class but it cannot call the methods which are newly declared in the sub class. In the above example the object eng was able to call the result()(overriding method) and departments()(non-overridden method of base class). The e.result() will call CSE version of type method. Because here e refers to CSE object, cse (that means e = cse;). However if you try to call the disp() method (which has been newly declared in CSE class) [eng.disp()] then it would give compilation error.


Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method disp() is undefined for the type Engineering

Rules for method overriding:
  • Method should have same name as in the super class.

  • In java overriding, the argument list of the overriding method should be exactly the same as that of the method in super class. The data types of the arguments and their sequence should be maintained as it is in the overridden method.

  • The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.

  • The Access Modifier of the overriding method of subclass cannot be more restrictive than the overridden method of parent class. For e.g. if the Access Modifier of base class method is public then the overriding method in sub class cannot be either private or protected.
    Example:
    method overriding


    in above example, display() method in sub class is more restrictive(protected) than base class(public). So it reports compilation error.


  • A method declared with the final, private keyword cannot be overridden.

  • Instance methods can be overridden only if they are inherited by the subclass.

  • A method declared static cannot be overridden but can be re-declared in the sub class, but the sub-class method would act differently and will have nothing to do with the same static method of parent class.

  • If a method cannot be inherited, then it cannot be overridden.

  • A subclass in a different package can only override the non-final methods declared public or protected.

  • An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method in sub class can throw fewer exceptions than the overridden method.

  • Constructors cannot be overridden.

  • If a class is extending an abstract class then it has to override all the abstract methods unless the class itself is a abstract class.

Super keyword :

super keyword is used for invoking the parent class method/constructor. For calling base class method, use super.methodname(). Constructor of base class is called by super(). Let’s see the use of super in Overriding.


Example:
method overriding


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status