课程: Java Object-Oriented Programming
今天就学习课程吧!
今天就开通帐号,24,600 门业界名师课程任您挑!
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…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
-
-
(已锁定)
What is inheritance?2 分钟 41 秒
-
(已锁定)
Leveraging different types of inheritance2 分钟 49 秒
-
(已锁定)
Using inheritance to reduce code duplication4 分钟 51 秒
-
(已锁定)
Discovering inheritance in foundational Java classes3 分钟 18 秒
-
(已锁定)
Challenge: Reduce redundant code with inheritance1 分钟 51 秒
-
(已锁定)
Solution: Reduce redundant code with inheritance4 分钟 37 秒
-
(已锁定)
-
-
-