how2examples.com
Home - Introduction to Java Programming

Java Inheritance

Inheritance provides a mechanism for reuse by allowing a new class (the subclass) to extend an existing class (the superclass or parent class). The subclass inherits the superclasses functionality.

The extends keyword

Java supports the object orientated concept of inheritance using the extends keyword. In Java a class may only directly extend one other class but, as that class may also extend another class, a class can have many superclasses.

Method Overriding

A class can modify the behaviour it inherits from it superclasses by using method overriding. Method overriding is achieved by redefining in the subclass a method defined in a superclass. There are certain requirements that need to be met in order for a subclass to override a superclass method:

The Override Annotation

The Override annotation can be applied to a method to indicate the method overrides a version in a superclass. This annotation can help identify at compile-time incorrect attempts to override a method that might not otherwise by noticed until runtime.

Contents of OverrideAnnotationExample.java:
public class OverrideAnnotationExample {
   // incorrect attempt at overriding Object.toString()
   @Override
   public String getString() {
      return "OverrideAnnotationExample";
   }
   
   // incorrect attempt at overriding Object.equals(Object)
   @Override
   public boolean equals(OverrideAnnotationExample o) {
      return this==o;
   }
   
   // incorrect attempt at overriding Object.hashCode()
   @Override
   public int hashcode() {
      return 1;
   }
}
Command to compile OverrideAnnotationExample.java:
javac OverrideAnnotationExample.java
Result of trying to compile OverrideAnnotationExample.java:
OverrideAnnotationExample.java:3: method does not override or implement a method from a supertype
   @Override
   ^
OverrideAnnotationExample.java:9: method does not override or implement a method from a supertype
   @Override
   ^
OverrideAnnotationExample.java:15: method does not override or implement a method from a supertype
   @Override
   ^
3 errors

Late Binding

The particular version of a method to invoke is determined at runtime. The version to invoke is based on the class of the object the method is called on rather than the type of a variable that refers to it. Delaying the decision until runtime (a process called late binding or virtual method invocation) allows various subclasses to be used interchangeably.

Contents of LateBindingExample.java:
public class LateBindingExample {
   public static void main(String[] args) {
      A o1 = new A();
      A o2 = new B();
      B o3 = new B();
      System.out.println(o1.x() + o2.x() + o3.x());
   }
}

class A {
   String x() {
      return "A.x()";
   }
}

class B extends A {
   @Override
   String x() {
      return "B.x()";
   }
}
Command to compile LateBindingExample.java:
javac LateBindingExample.java
Command to run LateBindingExample:
java LateBindingExample
Output from running LateBindingExample:
A.x()B.x()B.x()

Invoking Overriden Methods

Overriding a method replaces the superclass version with the subclass version. However, sometimes it may be useful for a subclass version to reuse an overridden method of a superclass. This is possible using the super keyword.

Contents of SuperExample.java:
public class SuperExample {
   public static void main(String[] args) {
      B o = new B();
      o.x();
   }
}

class A {
   void x() {
      System.out.print("A.x()");
   }
}

class B extends A {
   @Override
   void x() {
      System.out.print("start ");
      super.x();
      System.out.print(" finish");
   }
}
Command to compile SuperExample.java:
javac SuperExample.java
Command to run SuperExample:
java SuperExample
Output from running SuperExample:
start A.x() finish

Constructor Chaining

See invoking superclass constructors.

Abstract Classes

Java supports the concept of abstract classes. Abstract classes are specifically intended to be subclassed rather than instantiated directly. Abstract classes can define abstract methods and leave subclasses to provide the implementation. Abstract classes and methods are specified using the abstract keyword. If a class has an abstract method then the class must also be abstract. If a non-abstract class extends an abstract class it must also implement all of the superclasses abstract methods.

Contents of AbstractExample.java:
public class AbstractExample {
   public static void main(String[] args) {
      A o = new B();
      o.x();
   }
}

abstract class A {
   void x() {
      System.out.print("A.x() " + y());
   }
   
   abstract String y();
}

class B extends A {
   @Override
   String y() {
      return "B.y()";
   }
}
Command to compile AbstractExample.java:
javac AbstractExample.java
Command to run AbstractExample:
java AbstractExample
Output from running AbstractExample:
A.x() B.y()