Inheritance in Java: Definition, Example & Syntax

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Inheritance vs. Polymorphism in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:03 A Great Inheritance
  • 1:05 Syntax
  • 2:40 Overriding
  • 4:59 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
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.

Inheritance in Java is not an estate or a classic car from a long-lost relative. It means certain classes can share attributes from other classes. Here, we'll learn the concept and the syntax for this powerful feature of Java.

A Great Inheritance

Inheritance is a wonderful thing in Java. It's a term used often in object-oriented programming. But what does it really mean? And how do you harness this powerful concept?

To inherit in Java means allowing all methods and variables from one class to be accessible by another class. That is, the new class inherits these items. The parent class, also called superclass, is the class whose methods and variables can be used in the child class (also called subclass).

Another way to think of the inheritance concept is the phrase: A union employee is an employee; a paperback book is a book. It's more than a child inheriting 50% of their parent's DNA - they get all of it. The subclass gets all the goodies from the parent class, but it can also create its own variables and methods.

Inheritance is a key part of object-oriented programming. It allows for use and re-use of objects, methods, and variables, without having to add extra/redundant code.

Syntax

The syntax for creating a subclass, and thus harnessing inheritance is:

public class Class extends ParentClass {
  //new variable or methods here
}

Inheritance in Java: Example

Let's look at some code. The following example creates an Employee class and another class for a union employee. All of the features of the employee class come over to the Union class, such as pay, FTE (full-time equivalent; how many hours you work in a pay period). However, this class can be used for its own methods.

public class Employee {
  private double payRate;
  private String fullName;
  private double FTE;
  public void calculatePay() {
   //calculate the pay here
  }
}
public class UnionEmployee extends Employee {
  // everything from Employee will come over
  // new fields for UnionEmployee:
  private String barganingUnit;
  private String unionCode;
  private double unionDues;
  public void calculateUnionDues() {
   // can use the Employee calc pay
   // also special calc for dues
  }
}

The subclass can use the variables and methods from the parent class, but not vice versa. You are not able to bring in the union information to the parent Employee class as they are private to the UnionEmployee class.

Overriding

The child class inherits variables and methods from the parent. Not only can you use the methods from the parent class, you can override, or change, them. In the above example, we could use the method to calculate pay as-is. Or, we could override it for a union employee.

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