a class inherits from a single class, as in the case of BankAccount inheriting
from Asset, it is called single-level inheritance or simply single inheritance.
In single inheritance, we create a class that inherits the properties of
another single class. We may create multiple classes that inherit from a single
class. This was stated earlier when we said that bank account, security, and real
estate are types of assets. Thus, all these can be represented as classes
inheriting from the Asset class, as shown here:
that the newly defined classes Security and RealEstate both extend the Asset
class and define the fields unique to each. Just like the BankAccount class, both
the Security and RealEstate classes will have access to the base class fields
a class inherits from a single class, known as the parent class, as in the case
of the BankAccount, Security, and RealEstate classes inheriting from Asset, it
is called single inheritance. If the class itself does not inherit from any
other class, we call it a top-level class.
will now extend our single-level class hierarchy to multiple levels. A
BankAccount inherits from Asset. A bank account can be one of two types:
savings or checking. Therefore, we can say the following:
A bank account “is an”
A savings account “is a”
type of bank account.
A checking account “is a”
type of bank account.
we could add two more classes, called SavingsAccount and CheckingAccount, to
our class hierarchy to represent these additional real-life entities. This is
shown in the UML diagram here:
savings account draws monthly interest, but the bank does not pay any interest
on the checking account. Thus, the SavingsAccount class defines a field called
interestRate, whereas the CheckingAccount does not. Likewise, CheckingAccount
has a unique field or attribute called overdraftLimit, which is missing in the
SavingsAccount class. To get a better grasp of these inheritance hierarchies.
Java, every class inherits from the Object class. Thus, Object is a top-level
class.Also, one of the most important thing to be discussed about inheritance
is that, we may be wondering whether it is possible to inherit from multiple
classes. C++ allows us to inherit from multiple classes. However, Java does not
support multiple inheritance. This means a Java class cannot simultaneously
inherit the characteristics of two or more Java classes. Java has interfaces,
which provide a sort of multiple inheritance.