Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces

Inheritance: Basics

Definition, Concept, Advantages, Syntax

Definition: Inheritance is a mechanism in Java by which derived class can borrow the properties of base class and at the same time the derived class may have some additional properties.

Inheritance: Basics

• Definition: Inheritance is a mechanism in Java by which derived class can borrow the properties of base class and at the same time the derived class may have some additional properties.

• The inheritance can be achieved by incorporating the definition of one class into another using the keyword extends.

Advantages of Inheritance

One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code amongst several subclasses.

1. Reusability: The base class code can be used by derived class without any need to rewrite the code.

2. Extensibility: The base class logic can be extended in the derived classes.

3. Data hiding: Base class can decide to keep some data private so that it cannot be altered by the derived class.

4. Overriding: With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class

Concept of Base and Derived Class

• The inheritance is a mechanism in which the child class is derived from a parent class.

• This derivation is using the keyword extends.

• The parent class is called base class and child class is called derived class.

•For example

Class A

This is Base class

{

………

}

Class B extends A

This is Derived class

{

……….// uses properties of A

}

• Inheritance is represented diagrammatically as follows


Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : Definition, Concept, Advantages, Syntax - Inheritance: Basics