How does Java implement polymorphism

How does Java implement polymorphism

Answer: To demonstrate polymorphic features example give below where we are Pizza class with ChickenPizza and VegitablePizza and adding extra field toppings in both child class:

package com.javahonk.polymorphism;

public class Pizza {
	String dough;
	String sauce;
	String cheese;

	public Pizza(String dough, String sauce, String cheese) {
		super();
		this.dough = dough;
		this.sauce = sauce;
		this.cheese = cheese;
	}

	public void getMyPizza() {
		System.out.println("Your Pizza. Dough: " + dough +
				" Sauce: " + sauce
				+ " Cheese: " + cheese);
	}

}

 

package com.javahonk.polymorphism;

public class ChickenPizza extends Pizza{

	String toppings;

	public ChickenPizza(String dough, String sauce, 
			String cheese,String toppings) {
		super(dough, sauce, cheese);
		this.setToppings(toppings);
	}

	public String getToppings() {
		return toppings;
	}

	public void setToppings(String toppings) {
		this.toppings = toppings;
	}	

	public void getMyPizza(){
		super.getMyPizza();
		System.out.println("Chicken Pizza toppings:"
		+toppings);
	}

}

 

Note the overridden getMyPizza method. In addition to the information provided before, additional data about the toppings is included to the output.

package com.javahonk.polymorphism;

public class VegitablePizza extends Pizza{

	String veggieToppings;

	public VegitablePizza(String dough, String sauce, 
			String cheese, String veggieToppings) {
		super(dough, sauce, cheese);
		this.setVeggieToppings(veggieToppings);
	}

	public String getVeggieToppings() {
		return veggieToppings;
	}

	public void setVeggieToppings(String veggieToppings) {
		this.veggieToppings = veggieToppings;
	}

	public void getMyPizza(){
		super.getMyPizza();
		System.out.println("Vegitable Pizza toppings:"
		+veggieToppings);
	}

}

 

Note that once again, the getMyPizza method has been overridden. This time again we have added vegibleToppings.

To summarize, there are three classes: Pizza, ChickenPizza, and VegitablePizza. The two subclasses override the getMyPizza method and print pizza information.

Here is a BuildPizza test program that creates three Pizza variables. Each variable is assigned to one of the three Pizza classes. Each variable will be then printed.

package com.javahonk.polymorphism;

public class BuildPizza {

	public static void main(String[] args) {

		Pizza chickenPizza, vegitablePizza;

		chickenPizza=new ChickenPizza("Classic", 
				"Pizza Sause", "Normal", "Chicken");
		vegitablePizza=new VegitablePizza("Modern", 
				"Pizza Sause", "Medium", "Vegitable");

		System.out.println("Your chicken pizza details:\n");
		chickenPizza.getMyPizza();
		System.out.println("\nYour vegitable pizza details:\n");
		vegitablePizza.getMyPizza();

	}

}

 

The JVM calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable’s type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.

Output:

How does Java implement polymorphism

 

Leave a Reply

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

I am not Robot *