Method Overriding Rules and Scope in Java : Free Java Tutorials

Method overriding is as important as method overloading and a slightly tricky concept of Java. Whenever we do method overriding, we must follow some rules and take care of the scope of access modifiers. We will discuss these things in detail in this article.

Method overriding scope and rules related access modifiers

Private methods of parent class are not available to the child class and hence overriding concept is not applicable to private methods.

However, we can define the exact same private method in the child class to satisfy our requirement. Although this is perfectly valid, it is not overriding.

// Parent class
class Parent
{
	// private method of Parent class
	private void method()
	{
		System.out.println("Parent class method is executing");
	}
}

// Child class
class Child extends Parent
{
	// private method of Child class
	private void method()
	{
		System.out.println("Child class method is executing");
	}
}

In the above code, child class’ method() is actually not an overriding method but it is a separate private method of the child class.

We cannot override the parent class final methods in child classes. If we try to override such methods, we will get a compile-time error saying that overridden method is final.

 // Parent class
class Parent
{
	// final method of Parent class
	public final void method()
	{
		System.out.println("Parent class method is executing");
	}
}

// Child class
class Child extends Parent
{
	// overriding abstract method of Parent class
	public void method()
	{
		System.out.println("Child class method is executing");
	}
}

The above code will give a compile-time error saying Cannot override the final method from Parent.

Parent class abstract methods should be overridden in the child class to provide the implementation.

// Parent class
abstract class Parent
{
	// abstract method of Parent class
	public void method()
	{
		System.out.println("Parent class method is executing");
	}
}

// Child class
class Child extends Parent
{
	// overriding abstract method of Parent class
	public void method()
	{
		System.out.println("Child class method is executing");
	}
}

While overriding, we cannot reduce the scope of access modifier. But we can increase the scope.

// Parent class
class Parent
{
	// method of Parent class
	public void method()
	{
		System.out.println("Parent class method is executing");
	}
}

// Child class
class Child extends Parent
{
	// overriding method of Parent class
	void method()
	{
		System.out.println("Child class method is executing");
	}
}

This code will give a compile-time error saying Cannot reduce the visibility of the inherited method from Parent since we are attempting to assign weaker access privileges.

Order of access modifiers from most restricted to least restricted:

private -> default -> protected -> public

Based on this order, we can override parent class method without assigning child method more restricted access modifier.

We can override non-abstract methods as abstract. The main advantage of this approach is that we can stop the availability of parent level implementation to the next level child classes.

// Parent class
class Parent
{
	// method of Parent class
	public void method()
	{
		System.out.println("Parent class method is executing");
	}
}

// Child class
abstract class Child extends Parent
{
	// overriding method of Parent class by an abstract method
	public abstract void method();
}

This was all about Method overriding rules that we must follow in Java. Hope you find this tutorial helpful.

Leave a Comment

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