课程: Java Object-Oriented Programming

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Using inheritance to reduce code duplication

Using inheritance to reduce code duplication - Java教程

课程: 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…

内容