Inheritance vs. Polymorphism in Java

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology 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().

Inheritance

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
}


Polymorphism

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 Study.com 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 Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support