Java Programming
About Lesson

What is Inheritance?

Inheritance describes the ability of one class or object to possess characteristics and functionality of another class or object in the hierarchy.
Inheritance is a language property specific to the object-oriented paradigm. Inheritance is used for a unique form of code-sharing by allowing you to take the implementation of any given class and build a new class based on that implementation.
For example class B, starts by inheriting all of the data and operations defined in the class A. This new subclass can extend the behaviour by adding additional data and new methods to operate on it. Basically during programming inheritance is used for extending the existing property of a class. In other words it can be said that inheritance is “from generalization- to-specialization”. In this by using general class, class with specific properties can be defined.

How inheritance can be created in java?

To inherit a class into another class extends keyword is used. For example, SavingAccount class will inherit BankAccount class as given below.

class BankAccount
{
data members
member functions
}
class SavingAccount extends BankAccount
{
data members
member functions
}

 

Super Class and Derived Class

A class derived from the superclass is called the subclass. Sometime superclass is also called parent class or base class and subclass is called as child class or derived class. The subclass can reuse the data member and methods of the superclass that were already implemented and it can also extend or replace the behaviour in the superclass by overriding methods. Subclass can have its own data members and member functions.

In any type of inheritance, it is usually we create object from the down level class since it is most specialised in the hierarchy. But it does not mean that we cannot create object from parent classes in the inheritance.

You cannot copy content of this page