मराठी

Inheritance

Advertisements
  • Introduction to Inheritance 
  • Single Inheritance 
  • Multiple Inheritance 
  • Multilevel Inheritance 
  • Hierarchical inheritance 
  • Hybrid inheritance 

Notes

Inheritance stands for derivation. The mechanism of deriving a new class from an old one is called inheritance. The old class is referred to as the base class and new one is called the derived class. 

Types of Inheritance 

a) A derived class with only one base class is called single inheritance.

b) A derived class with several base classes is multiple inheritance. 

c) When more than one class is derived from one base class then it is hierarchical inheritance. 

d) The mechanism of deriving a class from another 'derived class' is known as multilevel inheritance. 

e) When there is more than one type of inheritance used, it is called hybrid inheritance. 

  Defining derived classes 

A derived class is defined by specifying its relationship with the base class. The general form is: 

class derived-class-name : visibility -mode base-class-name  

        { 

               - - -members of derived class  

        } ; 

The colon indicates that the derived class is derived from the base class. The optional visibility mode (private, protected, or public) defaults to private and specifies how base class features are inherited. In private inheritance, base class public members become private in the derived class. In public inheritance, they remain public. Private members are inaccessible to derived classes; protected members are accessible only within derived classes. 

Virtual base classes 

 

The' child' has two direct bases 'parent 1' and 'parent 2' which themselves have a common base class 'grandparent'. Here 'child' would have duplicate sets of the member inherited from 'grandparent' via 'parent1' and again via 'parent 2'. This introduces ambiguity. It can be avoided. The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual base class. 

Constructors in derived classes 

It is required for the derived class to have a constructor and send arguments to the base class constructor if the base class has a constructor with one or more parameters. When a constructor is present in both the base and derived classes, the base constructor is used first, followed by the derived class constructor.

Inheritance:

Inheritance in Java allows a class to inherit properties and methods from another class, thus allowing the reusability of code between various methods of different class. This establishes a parent-child relationship between classes.  

Visibility Mode of Inheritance 

A base class can be derived by another class by using only the public mode of inheritance. 

Public: By using the public mode, the Public and Protected members of the Base class act as the Public members of the derived class. The syntax used in Public Inheritance is given below:  

public<derived class>extends<base class> 

Using Data Members Protected in the Base Class 

Class members are declared private for restricted access within the class. A class member declared private cannot be accessed by any of the derived class. For inheritance, base class members are declared protected, allowing direct access by derived classes.

If you would like to contribute notes or other learning material, please submit them using the button below.
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×