Inheritance in Java
In this lesson, you'll learn about an important concept of Object Oriented programming known as Inheritance.
We'll cover the following...
What is inheritance? #
Inheritance
provides a way to create a new class from an existing class. The new class is a specialized version of the existing class such that it inherits elements of the existing class.
Terminology #
- Superclass(Parent Class): This class allows the re-use of its members in another class.
- Derived Class(Child Class or Subclass): This class is the one that inherits from the superclass.
Hence, we can see that classes can be built by using previously created classes!
Notation #
Let’s take a look at the notation necessary for the creation of subclasses. This is done by using the keyword extends
.
Press + to interact
class Student{public String name;public int age;}class Undergrad extends Student{String major;public Undergrad(){this.major = "Computer"this.name="John Doe";this.age=50;}}
Inheritance establishes an " is an" relationship ...