Inheritance in Java

The following are different relationships between classes. Let A and B be two classes.

  • Dependency: A uses B
  • Aggregation: A has-a B (as an attribute)
  • Inheritance: B is a kind of A

Subclass and Superclass

In Java, new classes (child classes, subclasses) can be derived from existing classes (parent classes, superclasses), thereby inheriting fields and methods from the superclasses. The subclass is defined with the extends keyword.

When a superclass is not specified explicitly, by default, the java.lang.Object serves as the superclass. All classes are ultimately derived from the Object class, and this forms a hierarchy of classes.

A subclass inherits all members (fields and methods) from its superclass. This allows the subclass to reuse the existing code.

In the following, we define Student as a subclass of Person.

public class Person
    protected String name;

    public Person(String a)
    {   name = a;   }
    public void setName(String a)
    {   name = a;   }
    public String getName()
    {   return name;   }

public class Student extends Person
    private int studentNo;

    public Student(String a, int n)
    {   name = a; studentNo = n;  }
    public void setStudentNo(int n)
    {   studentNo = n;   }
    public String getStudentNo()
    {   return studentNo;   }

Visibility Modifiers

Visibility modifiers for members of a superclass affect how they can be accessed in a subclass.

  • public and protected members of a superclass are visible in its subclasses.
  • protected members of a superclass are not visible in other classes which are not subclasses.
  • private members are not visible in the subclasses. They are usually accessed indirectly via public and protected methods.

this and super References

There are two special references in Java, called: this and super. this refers to the object itself, and super refers to the parent class. For example we can define the constructors of Student in the following way.

    public Student(String name, int studentNo)
        this.studentNo = studentNo;

Here super() refers to the constructor of the superclass, and this.studentNo refers to the studentNo of the current object.


In the subclass, we may define a method with the same name and signature as in the superclass. This is called an overriding method. For example, the toString() method of the Object class is usually overridden to give a meaningful description of an object. In the following code, we define the toString() method for the Student class; it overrides the method in Object.

    public String toString()
        return "Name: " + name + ", Student No: " + studentNo;

Overriding deals with methods from different classes: superclasses and subclasses. An overriding method has the same name and signature as in the superclass. It allows defining a similar operation in different ways for different object types (parent and child classes).

In contrast, overloading deals with multiple methods with the same name in the same class, but they have different signatures. Overloading allows defining a similar operation in different ways for different parameters (in one class).


All classes are ultimately derived from the java.lang.Object class. Public methods of the Object class are inherited by every Java class. The following are some examples:

  • boolean equals(Object obj) ¬†Indicates whether obj “equals” the current object
  • String toString() ¬†Returns a string representation of the object

Abstract Classes and Methods

An abstract class represents a generic concept. No object can be created from an abstract class. An abstract method has no definition. Any class with abstract methods must be declared as an abstract class. For example,

public abstract class GraphicObject
    protected int  x, y;

    public void moveTo(int _x, int _y)
    {  x = _x; y = _y; }

    public abstract void draw();
    public abstract void resize();
public class Circle extends GraphicObject
    int  radius;
    public void draw()
    { ... }
    public void resize()
    { ... }

Final Classes and Methods

The final modifier is used to define an entity which cannot later be changed. It restricts inheritance.

  • A final class cannot have subclasses
  • A final method cannot be overridden
  • A final variable can only be initialize once

Many classes in the Java library are final: java.lang.String, java.lang.System.

public final class MyFinalClass {...}

public class ThisIsWrong extends MyFinalClass {...} // THIS IS WRONG!!! 

public class MyClass
    public void myMethod() {...}
    public final void myFinalMethod() {...}
public class AnotherClass extends MyClass
    public void myMethod() {...} // OK
    public final void myFinalMethod() {...} // THIS IS WRONG!!! 

A final variable can only be initialized once, either by an initializer or an assignment

public class Sphere
    public static final double PI = 3.141592653;
    public final double radius;
    Sphere(double r) { radius = r; }



Tagged with: , , , , , ,
Posted in Java