Home > Java > Method Overriding in Java – PART-3

Method Overriding in Java – PART-3

Rules of method overriding in Java

  • Argument list: The argument list of overriding method must be same as that of the method in parent class (overridden). The data types of the arguments and their sequence should be maintained as it is in the overriding method.
  • Access Modifier: The Access Modifier of the overriding method (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 (child class method ) cannot have private, protected and default Access modifier as all of the three are more restrictive than public.

In the below example as child class disp method is more restrictive (protected) than base class (public)

However this is perfectly valid scenario as public is less restrictive than protected. Same access modifier is also a valid one.

 

  • Private, static and final methods cannot be overridden as they are local to the class (because, a static method is bounded with class). However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.
  • Overriding method (method of child class) can throw any unchecked exceptions, regardless of whether the overridden method (method of parent class) throws any exception or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. We will discuss this in detail with example in the upcoming tutorial.
  • Binding of overridden methods happen at runtime which is known as dynamic binding.
  • If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is an abstract class.
  • Instance methods can be overridden only if they are inherited by the subclass (instance method is bounded with object).
  • 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 can throw narrower or fewer exceptions than the overridden method.
  • Constructors cannot be overridden.
  • Method must have same name as in the parent class.
  • Method must have same parameter as in the parent class.
  • Must be IS-A relationship (inheritance).
  • Return type must be same or covariant in method overriding.

Refer Part-1 for basic overriding concepts

Refer Part-2 for overriding concepts with examples.

This Article is TAGGED in , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">