📕
Knowledge
  • Knowledge Repository
  • Java
    • Intro
    • Persistence
      • Java Persistence API (JPA)
        • Entity Manager
        • Transaction Management
        • Relationship Mapping
      • Java Transaction API (JTA)
    • Resources
  • Angular
    • Intro
    • Overview
    • CLI Commands
    • Directives
  • Tools
    • IDEs
      • IntelliJ
        • Shortcuts
  • Book Notes
    • Intro
    • Java A Beginner's Guide
      • Chapter 1. Java Fundamentals
      • Chapter 2. Data Types and Operators
      • Chapter 3. Program Control Statements
      • Chapter 4. Introducing Classes, Objects, and Methods
      • Chapter 5. More Data Types & Operators
      • Chapter 6. Closer Look At Methods and Classes
      • Chapter 7. Inheritance
      • Chapter 8. Packages & Interfaces
      • Chapter 9. Exception Handling
      • Chapter 10. Using I/O
    • Data Pipelines With Airflow
      • Chapter 1. Meet Airflow
      • Chapter 2. Anatomy Of a DAG
  • Course Notes
    • Intro
    • Spring: TDD With JUnit
Powered by GitBook
On this page
  • Basics
  • Member Access
  • Constructors and Inheritance
  • Using super() and super
  • super() for Superclass Constructors
  • super for Accessing Superclass Members
  • Using Abstract Classes
  • Using Final
  • The Object Class

Was this helpful?

  1. Book Notes
  2. Java A Beginner's Guide

Chapter 7. Inheritance

One of the three foundational principles of OOP, inheritance allows you to create a general class that defines shared traits common to a set of other classes which they can inherit from. A class that is inherited is called a superclass, and the class does the inheriting is called the subclass.

Basics

For a class to inherit from an other, it must extend the superclass by using the extend keyword.

public class subclass extends superclass() {
    ////
    Here you can now access properties of the superclass.
}

A class can only inherit from a single superclass, but you can create a hierarchy where a subclass becomes a superclass for another class.

Member Access

Inheriting from a superclass does not override any members declared with private. So if the superclass contains private methods, the subclass won't be able to access these.

Constructors and Inheritance

In an inheritance hierarchy when multiple constructors are involved, which constructor is in charge of building the object? The super or subclass? It's actually both. Each constructor becomes responsible for creating the class it belongs too.

Using super() and super

super() for Superclass Constructors

If you need to instantiate the parent class while creating the subclass object, you can use the super() constructor call in the subclass.

class Animal {
 
    // Constructor of superclass
    Animal()
    {
        System.out.println("Animal class Constructor");
    }
}

class Dog extends Animal {
    Dog()
    {
        super();
        System.out.println("Dog class Constructor");
    }
}

When executed, output will be:
Animal class Constructor
Dog class Constructor

Note: when calling super(), it must always be the first statement executed inside a subclass constructor.

super for Accessing Superclass Members

You can use the keyword super, in a similar way as this, except in the super case it refers to the superclass and allows you to access the members of a superclass using dot notation.

super.member

Member can be either a method or variable.

Using Abstract Classes

To create a superclass that only defines a general form to be shared across all subclasses use an abstract class. This is usually the case when the superclass is unable to create a meaningful implementation of a method that can be shared.

An abstract method will have an empty body and must be overridden in the subclass.

// Abstract class
abstract class Animal {

  public abstract void animalSound(); - // Abstract method (does not have a body)
}

class Pig extends Animal {
  
  // The method is overidden here.
  public void animalSound() {
    System.out.println("The pig says: oink");
  }
}

Couple of points to remember:

  1. Abstract methods have no body.

  2. If a class declares an abstract method, then it must become an abstract class.

  3. Abstract methods can only be applied to instance methods, not statics or constructors.

  4. You cannot instantiate an abstract class. You'll receive compilation error.

  5. Subclass must override all asbtract methods.

  6. The abstract class can contain methods that are not abstract to be shared across subclasses.

Using Final

If you need to prevent a method from being overridden you can use the final keyword. To do so, just specify final as the modifier at the start of the declaration.

If you want to prevent inheritance completely then you can declare a class as final, stopping any subclass from being able to inherit it.

You can also apply final to variables which creates a named constant. This makes the variable immutable after initialization. A typical convention is to apply static to final variables so they can be accessed via their class name rather than the through an object. final constant variables names are also typically UPPER_CASED.

The Object Class

There's one special class in Java called Object which is a superclass of all other classes created. So any class that you create is a subclass of Object. This also means that you're class always inherits a base set of methods. The most frequently used and overridden are hashCode(), equals() and toString().

PreviousChapter 6. Closer Look At Methods and ClassesNextChapter 8. Packages & Interfaces

Last updated 3 years ago

Was this helpful?