From the course: Java Object-Oriented Programming

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Using inheritance to reduce code duplication

Using inheritance to reduce code duplication - Java Tutorial

From the course: Java Object-Oriented Programming

Using inheritance to reduce code duplication

- [Instructor] In Java, we can achieve inheritance by using the keyword extends. The subclass will use this keyword with the superclass in its class definition to inherit all the properties and behaviors of the superclass. Let's take a look at an example in Java. Here we have a Salesperson class with name, salary, age and commissionPercentage attributes. We have a constructor, getter methods and methods to raise the salary and commission. In addition to the Salesperson class, we also have the Analyst class. An Analyst has some of the attributes and behaviors as the Salesperson class but some are different. For example, an Analyst has the concept of an AnnualBonus, which does not exist for the Salesperson class. But the Analyst also has the same name, age and salary attributes and behaviors. Instead of having the name, age and salary code duplicated across two classes, we can create an Employee class that holds this…

Contents