Inheritance is what happens when a subclass receives variables or methods from a superclass. It is also when one class passes down its properties to another class. The main reason for inheritance is reusability. Distinguishing a new class from an existing class is called derivation. The old class is referred to as the base class and the new class is called a derived class or sub class. The derived class inherits some or all of the traits from the base class.

A class can also inherit properties from more than one class, for more than one level.

Types of Inheritance

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multi level inheritance
  • Hybrid inheritance

Deriving a Class

Private derivation means that the derived class can access the public and protected base class members privately. With the privately derived class, the public and the protected base class members become privately derived class members.

Visibility of Inherited Members

Base class visibility Derived class visibility
Public Private Protected
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private protected

Java Inheritance Syntax

Java does not support multiple inheritance, except in the case of interfaces.

The following class definition causes ar15 to become a subclass of rifle.

public class ar15
 extends rifle {
 instance and class member definitions}