You can use the keyword within a method in a derived class

Core Concepts

Inheritance has the same core concepts in any object-oriented language
  • One class is derived from another, inherits all its data and methods
  • Terminology
    • superclass, base class, parent class
    • subclass, derived class, child class. Also called an extended class in Java

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 call to super() must be the first line of the derived class constructor
  • If explicit call to parent constructor not made, the subclass' constructor will automatically invoke super(). (the default constructor of the base class, if there is one)
  • Can also use super to invoke a method from the parent class (from inside the derived class). Format:

The protected modifier

  • Like in C++, protected data and methods of a public class can be accessed by any classes derived from the given class.
  • In Java, a protected member can also be accessed by any class in the same package

The final modifier

Use in Java for a few special inheritence-related purposes:
  • When used on a class declaration, it means that the class cannot be extended. (i.e. it cannot become a parent class to a new subclass)
  • When used on a method declaration, it means that the method cannot be overridden in a subclass. (i.e. this is the final version of the method)

Other differences

  • Java does not have multiple inheritance. A class can only extend one other class
    • C++ has multiple inheritance (a class can be derived from more than one base class
  • Java has a construction known as an interface. This is actually a keyword in Java. It allows behavior similar to inheritance, especially regarding polymorphism
  • In Java, every class is part of an inheritance hierarchy, because the class called Object (java.lang.Object) is automatically a base class to every Java class

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).


  • Suppose a class Rectangle is derived from class Shape.
  • Shape has a method:
      void Draw() { ... }  
  • We can define a method in class Rectangle with the same signature. The derived class version will override the base class version, when called through an object of type Rectangle.
      Rectangle r = new Rectangle();  // create a Rectangle object
    				  //  which has all the Shape methods available
      r.Draw();			  // invokes the Draw method from the
    				  //  Rectangle class
  • Note that the Rectangle class' Draw() method can still invoke the superclass' method, with the keyword super
      public void Draw()
        super.Draw();		// invoke parent's Draw()
        // continue with any processing specific to Rectangle

Abstract Classes

  • Like in C++, an abstract class is one that cannot be instantiated
  • To make a class abstract in Java, use the keyword abstract (which is a modifier)
      public abstract class Shape
  • Now that Shape is abstract, this would be illegal:
      Shape s = new Shape();	// specifically, it's  new Shape(); 
    				//   that is illegal
Java methods can be abstract as well:
  • An abstract method is a method signature without a definition
  • Abstract methods can only be created inside abstract classes
  • The main purpose of an abstract method is to be overridden in some derived class, or classes
  • Example:
      public abstract class Shape		// Shape is an abstract class
        public abstract double findArea();	// findArea is an abstract method
        // other methods and data
  • Note that a Java abstract method is like a pure virtual function in C++
  • In C++, a class is abstract if it has one or more pure virtual member functions. In Java, you just use the keyword abstract when declaring it

Which keyword is used to derive a class from another class?

A class can be derived from the base class in Java by using the extends keyword. This keyword is basically used to indicate that a new class is derived from the base class using inheritance.

Which method can be defined in the derived class?

A derived class has the ability to redefine, or override, an inherited method, replacing the inherited method by one that is specifically designed for the derived class.

Which keywords is used to derived from parent class?

Use Java's extends keyword to derive a child class from a parent class, invoke parent class constructors and methods, override methods, and more. Java supports class reuse through inheritance and composition.

When a class can be derived from another class and use its methods?

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).