Method overriding java examples using base and child class relationship, interface and abstract class with explanation.

We’ll learn how a child class can use base class behaviors / methods and override some of them in child class if don’t want to use base class methods without removing the methods from base class.

[Method Override definition– Writing the method in child class with same name, signature and return type, which is also in base class is known as method overriding. The child class can write its definition different that base class same method.]

Prerequisite:

Inheritance – parent child relationship in java

Let’s understand method overriding in java by example…

Let’s say a son is using his father’s home and car and he is using his own mobile. See the below program (No overriding concept) that implements this scenario. After that we will see the method overridden code.

The output of this java program will be as below

Output:

Father’s Home
Father’s Car
Son’s mobile

//base class
class Father {

	public void home() {
		System.out.println("Father's Home");
	}

	public void car() {
		System.out.println("Father's Car");
	}
}

// child class
class Son extends Father {

	void mobile() {
		System.out.println("Son's mobile");
	}
}

/*
 * Test
 */
public class TestOverriding {

	public static void main(String[] args) {

		Son s = new Son();
		s.home();
		s.car();
		s.mobile();
	}

}

Java code using method overriding

At later point of time, Son no longer wants to use his father’s Car. But, he still wants to use father’s home.

As a solution, Son will override Car() method and implement for own. So, the Car method with same name will be written in Son class and implemented. This is in fact method overriding concept.

Now the output will be as below. Notice here Son’s Car is called. In previous example it was Father’s Car.

Output:

Father’s Home
Son’s Car
Son’s mobile

/*
 * method overriding java example
 */
//base class
class Father {

	public void home() {
		System.out.println("Father's Home");
	}

	public void car() {
		System.out.println("Father's Car");
	}
}

// child class
class Son extends Father {
	@Override
	public void car() {
		System.out.println("Son's Car");
	}

	void mobile(){
		System.out.println("Son's mobile");
	}
}
/*
 * Test method overriding
 */
public class TestOverriding {

	public static void main(String[] args) {

		Son s = new Son();
		s.home();
		s.car();
		s.mobile();	}

}

DOUBT ON METHOD OVERRIDING

Doubt arises in mind that if we are going to write the same method and implement in child class, then why don’t we delete that method from the base class as we will no longer use that method from parent class.

Lets think of above program example, some later point of time, Son wanted to use his own Car and not his Father’s car. So, he overridden the Car method. Now answer yourself- Should the Son destroy his Father’s Car?

Off course not, some other child might be using that.

Similarly, You might use some third party base class, where you may not allow to change the class. Secondly, the parent class methods may be being used by other classes in the project/ applications that will not work.

So, It is not a good idea to remove method from a base class when you override that in child a class.

Let’s see another example i.e. a class implementing an interface , where you don’t have choice but implement and override the method. You can see an example of interface in java programming.

Prerequisite:

Interface concept in java

Method overriding java example using interface

In below java program, there is an interface Adapter. The class LaptopAdapter will implement the interface. The class will override the methods of interface and implement it.

Note that if a class is implementing an interface, then the class must override and define it in the class itself or else compiler will flash compile time error.

interface Adapter{
	public abstract void plugin();
}

class LaptopAdapter implements Adapter{

	@Override
	public void plugin() {
	
		System.out.println("Adapter plugged in");		
	}
	
}

public class Sample {

	public static void main(String[] args) {

		LaptopAdapter a = new LaptopAdapter();
		a.plugin();		
	}

}

Output:
Adapter plugged in

Example of Method Overriding in Java with Abstract Class

This is another example of overriding an abstract method of an abstract class. In below java program the abstract method engine() of Car class is being overridden in sub class Maruti.

In main() program, we have created an object of Maruti and call engine and run methods.

//Abstract class
abstract class Car{
	public abstract void engine();
	public void run() {
		System.out.println("Car is running...");
	}
}

//Extend abstract class car
class Maruti extends Car{

	@Override
	public void engine() {
		System.out.println("1000 cc");		
	}
	
}

public class Sample {

	public static void main(String[] args) {

		Maruti obj = new Maruti();
		obj.engine();
		obj.run();

	}
}

Output
1000 cc
Car is running…

NOTE:

Method overriding concept comes under run time polymorphism in java oops. Whereas, java constructor overloading and method overloading in java programming comes under compile time polymorphism.  You can read an interesting interview question – why method overriding is called run time polymorphims.

Related Posts