The Syntax and Terminologies
Learn how to use inheritance syntactically and the terminologies related to it.
We'll cover the following
The terminologies
A new class is created based on an existing class in Inheritance, hence we use the terminology below for the new class and the existing class:
- SuperClass (Parent Class or Base Class): This class allows the re-use of its
non-private
members in another class. - SubClass (Child Class or Derived Class): This class is the one that inherits from the superclass.
Note: A child class has all non-private characteristics of the parent class.
What does a child class have?
An object of the child class can use:
- All
non-private
members defined in the child class. - All
non-private
members defined in the parent class.
Note: Some classes cannot be inherited. Such classes are defined with the keyword,
final
. An example of such a class is the built-inInteger class
- this class cannot have derived classes.
The extends
Keyword
In Java, we have to use the keyword extends
to implement inheritance:
SubClass extends SuperClass{
//contents of SubClass
}
Let’s take an example of a Vehicle class
as a base class and implement a Car class
that will extend from this Vehicle class
. As a Car IS A, Vehicle the implementation of inheritance relation between these classes will stand valid.
Get hands-on with 1400+ tech skills courses.