Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Inheritance. No Comments on Method Overriding
5
(1)

In this article we will look at method overriding which is similar to method overloading. Method overriding is a mechanism which supports polymorphism in Java.

 

What is method overriding?

 

In the context of inheritance, suppose a base class A contains a method display with zero parameters and sub class B also contains a method display with zero parameters, what happens when we create an object for class B and call the display method using that object?

 

The method that will execute is the display method in the sub class B. Then what happened to the display method in super class A? It was hidden. This process of sub class method hiding the super class method when both methods contains same method signature is known as method overriding.

 

When is method overriding possible?

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Method overriding is possible only when the following things are satisfied:

  • A class inherits from another class (inheritance).
  • Both super class and sub class should contain a method with same signature.

 

What is the difference between method overloading and method overriding?

 

Both may look similar but they are quite different in the following ways:

  • Method overloading takes place when a class contains multiple methods with the same name but varying number of parameters or types of parameters.
  • Method overriding takes place when two or more classes contains a method with same signature and all of them are participating in inheritance.

 

Now let’s look at some examples which demonstrates method overriding.

 

Example 1:

class A
{
	public void display()
	{
		System.out.println("This is display method of class A");
	}
}

class B extends A
{
	public void display()
	{
		System.out.println("This is display method of class B");
	}	
}

public class Driver
{
	public static void main(String[] args) 
	{
		B obj = new B();
		obj.display();
	}
}

 

In the above example the method display in both classes A and B have same method signature (method name + number of parameters). So, the display method in class B hides the display method in class A. Output of the above program is:

 

This is display method of class B

 

Now let’s look at another example.

 

Example 2:

class A
{
	public void display()
	{
		System.out.println("This is display method of class A");
	}
}

class B extends A
{
	public void display()
	{
		System.out.println("This is display method of class B");
	}	
}

public class Driver
{
	public static void main(String[] args) 
	{
		A obj = new A();
		obj.display();
	}
}

 

In the above example, even though there are methods with the same signature, as the object obj belongs to class A, the display method of class A is executed and the output is:

 

This is display method of class A

 

Now let’s consider another example.

 

Example 3:

class A
{
	public void display()
	{
		System.out.println("This is display method of class A");
	}
}

class B extends A
{
	public void display(int x)
	{
		System.out.println("This is display method of class B");
	}	
}

public class Driver
{
	public static void main(String[] args) 
	{
		B obj = new B();
		obj.display();
	}
}

 

In the above example, notice that the display method in the sub class accepts a single integer parameter. Now when we call display method as obj.display() in the Driver class, method overloading takes place and display method of class A is executed. Output of the above program is:

 

This is display method of class A

 

Now let’s consider another example.

 

Example 4:

class A
{
	public static void display()
	{
		System.out.println("This is display method of class A");
	}
}

class B extends A
{
	public void display()
	{
		System.out.println("This is display method of class B");
	}	
}

public class Driver
{
	public static void main(String[] args) 
	{
		B obj = new B();
		obj.display();
	}
}

 

In the above example, the display method in the super class A has been declared as a static. You have to remember that static methods cannot be overridden. So, the above program gives a compile time error.

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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