Home     Blog

Inheritance

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 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}
VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • Java Method

    A Java method is a set of Java statements that can be included inside a Java class. Java methods are similar to functions or procedures in other programming languages. There are many different types of Java methods: Final Method Public Method Private Method Package Method Instance Method Protected Method Static Method Main Method The Main [...]...


  • Java vs. C++

    C++ supports pointers; Java does not (Java does have object references) C++ supports mutiple inheritance directly; Java supports multiple inheritance only through interfaces C++ supports operator overloading; Java does not C++ supports global variables; Java does not (sort of) C++ supports #define; Java does not C++ supports typedef; Java does not C++ supports enum; Java [...]...


  • Override

    Sometimes a subclass inherits a method from a superclass that doesn’t quite fit its needs. Perhaps the subclass inherited twenty methods and just one of them wasn’t quite right. In that case, the subclass would override that method by redefining that method itself. This override does not affect the method as defined in the superclass....


  • Java Class

    A Java class is a group of Java methods and variables. Each Java source code file can contain one public class. The name of this public class must match the name of the Java source code file. If the public class is called “ballistics,” then the filename would be “ballistics.java.” Example Java Class class Ammunition [...]...