What is Inheritance

What is Inheritance

Answer: Different kinds of objects often have a certain amount in common with each other. Chicken Pizza and Vegetable Pizza for example, all share the characteristics of Pizza (Dough, Sauce, Cheese). Yet each also defines additional features that make them different: Chicken Pizza would have chicken toppings and Vegetable Pizza will have vegetable topping.

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Below example, Pizza now becomes the super class of ChickenPizza and Vegetable Pizza. In the Java programming language, each class is allowed to have one direct super class, and each super class has the potential for an unlimited number of sub classes:

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 VegetablePizza extends Pizza{

	String veggieToppings;

	public VegetablePizza(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 veggieToppings.

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 two Pizza variables. Each variable is assigned to one of the two 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:

Core Java Interview questions series 5

 

Leave a Reply

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