Other Inheritance-related Keywords

Contact Us or call 1-877-932-8228
Other Inheritance-related Keywords

Other Inheritance-related Keywords


abstract states that the item cannot be realized in the current class, but can be if the class is extended. Note:

  • For a class, it states that the class can't be instantiated (it serves merely as a base for inheritance).
  • For a method, it states that the method is not implemented at this level.
  • The abstract keyword cannot be used in conjunction with final.

abstract Classes

abstract Classes are used when a class is used as a common point of maintenance for subsequent classes, but either structurally doesn't contain enough to be instantiated, or conceptually doesn't exist as a real physical entity.

public abstract class XYZ { ... }
  • We will make the Employee class abstract in the next exercise: while the concept of an employee exists, nobody in our payroll system would ever be just an employee , they would be exempt, nonexempt, or contract employees.
  • While you cannot instantiate an object from an abstract class, you can still create a reference variable whose type is that class.

abstract Methods

The method cannot be used in the current class, but only in a inheriting class that overrides the method with code.

public abstract String getPayInfo();
  • The method is not given a body, just a semicolon after the parentheses.
  • If a class has an abstract method, then the class must also be abstract.
  • You can extend a class with an abstract method without overriding the method with a concrete implementation, but then the class must be marked as abstract.


final is used to mark something that cannot be changed.

final Classes

The class cannot be extended.

public final class XYZ { ... }

final Methods

The method cannot be overridden when the class is extended.

public final void display() { ... }

final Properties

final Properties marks the field as a constant.

public static final int MAX = 100;
  • A final value can be initialized in a constructor or initializer:
public final double randConstant = Math.random();


public final double randConstant;
  • Then, in a constructor:
randConstant = Math.random();
  • Note: String and the wrapper classes use this in two ways:
    1. The class is final, so it cannot be extended.
    2. The internal field storing the data is final as well, but set by the constructor (this makes the instance immutable - the contents cannot be changed once set)
  • in some cases, a declaration of final enables the compiler to optimize methods, since it doesn't have to leave any "hooks" in for potential future inheritance.

Note that final and abstract cannot be used for the same element, since they have opposite effects.