Learn polymorphism in java with examples. You should have clear concept of polymorphism, types of polymorphism, method overloading, constructor overloading and method overriding. Compile time and run time polymorphism.

Definition of Polymorphism in Java

Polymorphism is a concept where one name can have many forms. It can be a method or a constructor.

In simple words,

having multiple methods or constructors with the same name in a class, or in a parent and child class.

Let’s understand in more simple way by example.

1)

A class in java can have multiple methods with same name having different parameters or data types. Hence, a method has many forms. This is known as method overloading.

Example:

Class Paint have 3 methods with same name, but, with different arguments.

class Paint {
	// all methods have same name
	public void Color(int x) {
	}

	public void Color(int x, int y) {
	}

	public void Color(int x, int y, int z) {
	}
}

2)

A class can has multiple constructors with different parameters in java program. All constructors share the same name as class. Hence, many forms of constructors. This is known as constructor overloading.

Example:

Employee. class has 3 constructors with different number of arguments or different data types if number of arguments are same. Notice that constructor name and class name is same. you can read constructors in java tutorial if you want.

class Employee {
	public Employee(int id) {
	}

	public Employee(String name) {
	}

	public Employee(int id, String name) {
	}

}

3)A base class and a child class have methods with same name. Therefore, method have many forms. This is known as method overriding.

Example:

Both, class Father and Son is having method car() with same name.

class Father {

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

// child class
class Son extends Father {

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

Types of Polymorphism in Java

There are two types polymorphism in java object-oriented programming i.e. overloading and overriding.

Method overloading, and Constructor overloading come under overloading polymorphism. Overloading is also known as compile time polymorphism.

Method overriding comes under overriding polymorphism and known as run time polymorphism.

So for, you understand what is Polymorphism in java oop, Now, read , method overloading , constructor overloading and method overriding in detail with examples. links below.

Compile Time and Run Time Polymorphism

Compile Time Polymorphism:

Now you understand that constructor overloading and method overloading comes under compile time polymorphism.  We say compile time polymorphism, because overloaded methods get resolved at compile time itself.

What does it mean, by get resolved?

It means, compiler understand which method to call, out of many overloaded methods during code compilation.

Run time Polymorphism:

Method overriding comes under run time polymorphism. In this method get resolve at run time. means, compiler will not be able to decide from which object’s methods will be called.

Read this interview Question what is compile time and run time polymorphism in java in details with code example.

Related Posts