X

Java Polymorphism Example

This example demonstrates the usage of polymorphism in Java programming language

What is Polymorphism

The term Polymorphism comes from the Greek language, and means “many forms”. Polymorphism in Java allows subclasses of a class to define their own unique behaviours and yet share some of the same functionality of the parent class. I’m going to discuss polymorphism from the point of view of inheritance where multiple methods, all with the same name, have slightly different functionality. This technique is also called method overriding.

Polymorphism is one of the four major concepts behind object-oriented programming (OOP). OOP questions are very common in job interviews, so you may expect questions about polymorphism on your next Java job interview.



Java Polymorphism Example

In this example we will create 3 classes to demonstrate polymorphism and one class to test the concept. Our superclass is called Animal. The successors of the animal class are Dog and Cat classes. Those are animals too, right? That’s what polymorphism is about – you have many forms of the same object with slightly different behaviour. To demonstrate this we will use a method called makeSound() and override the output of this method in the successor classes.

The pharmacy class uses objects of the drug and product class, for example, Cialis generic.

Java polymorphism example

The generalized animal class will output some abstract text when we call the makeSound() method:

package net.javatutorial;

public class Animal {
	
	public void makeSound() {
		System.out.println("the animal makes sounds");
	}

}

The Dog class, which extends Animal will produce slightly different result – the dog will bark. To achieve this we extend the Animal class and override the makeSound() method

package net.javatutorial;

public class Dog extends Animal{
	
	@Override
	public void makeSound() {
		System.out.println("the dog barks");
	}

}

Obviously we have to do the same to our Cat class to make the cat meow.

package net.javatutorial;

public class Cat extends Animal {
	
	@Override
	public void makeSound() {
		System.out.println("the cat meows");
	}

}

Finally lets test our creation.

package net.javatutorial;

public class PolymorphismExample {
	
	public static void main(String[] args) {
		Animal animal = new Animal();
		animal.makeSound();
		Dog dog = new Dog();
		dog.makeSound();
		animal = new Cat();
		animal.makeSound();
	}

}

First we create a general Animal object and call the makeSound() method. We do the same for a newly created Dog object. Now note the call to animal = new Cat() –  we assign a new Cat object to an Animal object. Cats are animals, remember? So, we can always do this:

Animal animal = new Cat();

By calling the makeSound() method of this object will actually call the overridden makeSound() method in the Cat class.

Finally, here is the output of the program

the animal makes sounds
the dog barks
the cat meows

 

References

Official Oracle polymorphism example

2.2 5 votes
Article Rating
filip:
Related Post