Inheritance and method overriding

In this post, In this post, we will discuss How do works inheritance and method overriding together. What are the method overriding rules in java and how we should take care of that? In this post, we will discuss all the method overriding rules in java with example.
Here is the table content of the article will we will cover this topic.
1. What is Method overriding with inheritance?
2. Rules of
Method overriding in inheritance?

What is Method overriding with inheritance?

In this article, we will discuss how we can achieve Method overriding in Java. If parent class and child class have a method with the same signatures, then its called method overriding.

Let’s say a parent class has a method sum(int, int) and a child class provides the specific implementation of the method (sum(int, int)) that has been declared in the parent class. It is known as method overriding.

class ParentClass
{
   // Overridden method
   public void printData()
   {
      System.out.println("In this method we are showing the details of Parent class");
   }
}
class ChildClass extends ParentClass
{
  	 //Overriding method
	public void printData()
	{
	     System.out.println("In this method we are showing the details of child class");
	     System.out.println("We are overriding the method");
	}
   public static void main( String args[]) {
	   ParentClass parentObject = new ChildClass();
      //This will call the child class method
	   parentObject.printData();
   }
}

Output:
In this method we are showing the details of child class
We are overriding the method

Advantage of method overriding:

Method overriding is only possible in inheritance. In method overriding the derived class can give its own specific implementation to an inherited method. You can provide the implementation of the method of derived class without even modifying the parent class code.

Rules of method overriding in inheritance

method overriding rules in java

1. Argument/Parameters list

The argument/parameter list of the overriding method (of child class) must be the same as the Overridden method(the method of parent class).

  • You can’t change number of arguments.
  • You can’t change the data type of arguments or their sequence should exactly match.

If you are changing anything in arguments of the method of the child class, then it is not considered method overriding.

2. Access modifier

The overriding method (method of child class) can’t be more restrictive than the overridden method (method of parent class). 

Example: If the Access Modifier of the overridden method (method of Parent class) is public then the overriding method (child class method ) cannot have a private, protected and default Access modifier because all of these three access modifiers are more restrictive than public. We can read Access Modifier from here

class ParentClass
{
   // Overridden method
   public void printData()
   {
      System.out.println("In this method we are showing the details of Parent class");
   }
}
class ChildClass extends ParentClass
{
  	 //Overriding method
	private void printData()
	{
	     System.out.println("In this method we are showing the details of child class");
	     System.out.println("We are overriding the method");
	}
   public static void main( String args[]) {
	   ChildClass childObject = new ChildClass();
      //This will call the child class method
	   childObject.printData();
   }
}

Output:
Exception in thread “main” java.lang.Error: Unresolved compilation problem: Cannot reduce the visibility of the inherited method from ParentClass at ChildClass.printData(ChildClass.java:12) at ChildClass.main(ChildClass.java:20)

3. Private methods can’t override

Private methods cannot be overridden as they are local to the class. You can’t inherit the private variables and methods of the parent class.

class ParentClass
{
   private void printData()
   {
      System.out.println("In this method we are showing the details of Parent class");
   }
}
class ChildClass extends ParentClass
{
   //Overriding method
	private void printData()
	{
	     System.out.println("In this method we are showing the details of child class");
	     System.out.println("We are overriding the method");
	}
   public static void main( String args[]) {
	   ParentClass obj = new ChildClass();
      //This will call the child class method
	   obj.printData();
   }
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem:     The method printData() from the type ParentClass is not visible at ChildClass.main(ChildClass.java:19)

We get compiler error printData() has private access in Parent class. So the compiler tries to call Parent class function, not child class, means printData() is not overridden.

4. static methods can’t override

If you have a static method in Parent class and you defining a static method with same signature as a static method in Parent class. In this case, the Child class method would act differently.  it is known as method hiding. 

  
class Parent 
{ 
    // Static method in base class 
    static void showData() 
    { 
        System.out.println("This is Static method of Parent class"); 
    } 
} 
  
class Child extends Parent 
{ 
	// static method in drived class
    static void showData()
    { 
        System.out.println("This is Static method of child class"); 
    } 
} 
  
class MainClass { 
    public static void main(String[] args) 
    { 
        Parent parent = new Child(); 
        parent.showData();      
    } 
}

Output: This is Static method of Parent class

As you know Static method can directly be called with class name. In this example we are calling showData() method by reference of Parent class then compiler converts.

5. final methods can’t override

final is a keyword in java used for restricting some functionalities. If we are declaring any method with final keyword it means we can’t change its implementation in child class. So, Java doesn’t permit to override any final method.

Example:

  
class Parent 
{ 
    // final method in base class 
    final void showData() 
    { 
        System.out.println("This is Static method of Parent class"); 
    } 
} 
  
class Child extends Parent 
{ 
	// final method in drived class
    final void showData()
    { 
        System.out.println("This is Static method of child class"); 
    } 
} 
  
class MainClass { 
    public static void main(String[] args) 
    { 
        Parent parent = new Child(); 
        parent.showData();        
    } 
}

Output: The above example will show compilation error

6. Overriding and constructor

You can’t overload the constructor of the parent class. Because the Constructor name must always be the same as for Class name.

Leave a Comment