Java Inheritance

Core Concepts

Inheritance has the same core concepts in any object-oriented language

Inheritance in Java

Use the keyword extends to declare the derived class
  // Example 1
  public class AAA		// AAA is the base class
  { ... }

  public class BBB extends AAA  // BBB is the derived class
  { ... }


  // Example 2
  public class Employee {...}				// base class
  public class HourlyEmployee extends Employee { ... }  // derived

keyword super

You can use super like a function call in a derived class constructor -- invokes the base class constructor
  super();		// invokes base class default constructor
  super(parameters);    // invokes base class constructor with parameters

  // Example, for a class called HourlyEmployee, derived from Employee
  public class HourlyEmployee extends Employee
  {
    public HourlyEmployee()	// default constructor
    {
	super();		// invokes Employee() constructor
    }

    public HourlyEmployee(double h, double r)
    {
	super(h,r);		// invokes Employee constructor w/ 2 parameters
    }
    
    // ... more methods and data

  } // end class HourlyEmployee

The protected modifier

The final modifier

Use in Java for a few special inheritence-related purposes:

Other differences


Method Overriding

Just like in C++, this is when a derived class has a method with the same prototype as a method in the base class. (The derived class function overrides the base class version, when called for a derived object).

Example:


Abstract Classes

Java methods can be abstract as well: