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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
What is inheritance?2m 41s
-
(Locked)
Leveraging different types of inheritance2m 49s
-
(Locked)
Using inheritance to reduce code duplication4m 51s
-
(Locked)
Discovering inheritance in foundational Java classes3m 18s
-
(Locked)
Challenge: Reduce redundant code with inheritance1m 51s
-
(Locked)
Solution: Reduce redundant code with inheritance4m 37s
-
(Locked)
-
-
-