Inheritance vs. Polymorphism in Java

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems, has a PhD in Information Technology Management, and a degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.

Cool concepts come with cool names: inheritance and polymorphism. In this lesson we'll cover how Java harnesses inheritance to create families of classes and explain how you can create methods without having to know what object you're using!

Object-Oriented Principles

Java is an object-oriented programming language. Therefore, everything is an object. An object, in turn, is an instance of a class. Think of a Sandwich: It basic components, such as bread or lettuce, and some sort of filling. When you create a tuna on rye, you are creating an instance of the sandwich class.

When making a tuna on rye, you may have to invoke a few methods; that is, some sort of action to actually create the instance of the sandwich class. In Java, the methods might look like assembleSandwich() or getIngredients().


Think of a generic Sandwich object: this is our matriarch of the family. Next we can have hot, cold or iced sandwiches. The following graphic shows a sample of what we can create:

Java inheritance family tree

One thing you cannot do, however, is inherit from multiple direct parents. This may work in biology but Java puts its foot down: Inheriting from multiple classes is called multiple inheritance. The Philly Cheese can't inherit from both Hot AND Cold. It can inherit from Hot. Hot can inherit from Sandwich.

In order to inherit from the parent, use the keyword extends in the code. The following examples show how each child class is created to inherit from the main Sandwich class:

public class HotSandwich extends Sandwich {
//inherit from Sandwich
public class PhillyCheese extends HotSandwich {
//inherit from HotSandwich


The concept of polymorphism may be confusing, but it can be just what it implies: The ability to change to different things. It is the shape-shifter concept of Java. And, true to it's form (pardon the pun), there are three flavors of polymorphism in Java.

Many Objects, One Name

From our Sandwich family, you can create new instances of any of the Sandwich objects. For example, the following code is perfectly acceptable. You have polymorphed/shape-shifted the mySandwich object because Java lets you create instances of ANY of the classes.

mySandwich = new HotSandwich();
mySandwich = new PhillyCheese();
mySandwich = new IceCreamSandwich();

Overloading: Many Methods, One Name

Think of the assembleSandwich() method for classes that are part of the Sandwich family. Each method is a little different (this concept is called overloading; when there are multiple methods with the same name but different actions).

public class ColdSandwich extends Sandwich {
 public double assembleSandwich(int cheeseSlices) {
  return cheeseSlices;
public class HotSandwich extends Sandwich {
 double toastTime;
 public double assembleSandwich(int cheeseSlices, double toastTime) {
  return toastTime;

As long as you call the assemble method and pass in the right arguments (number of cheese slices and the time for toasting), it will call the HotSandwich class. Even though we might have several assembleSandwich() methods, Java is smart enough to know which one we are using.

But what if we had two methods with the same name AND parameters?

Overriding: Same Name, Same Parameters

It's perfectly OK to have a HotSandwich class with an assembleSandwich() method AND a PhillyCheese class to have the same method. They can even have the same parameters! Take a look at the code snippet for both classes (remember that PhillyCheese inherits all of the data and methods from HotSandwich!) We'll create a couple of methods, each printing different text.

public class HotSandwich extends Sandwich {
 public void assembleSandwich() {
  System.out.println("Build hot");
public class PhillyCheese extends HotSandwich {
 public void assembleSandwich() {
  System.out.println("Build Philly");

Now, we need a main function to create new instances of these objects.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it now
Create an account to start this course today
Used by over 30 million students worldwide
Create an account