Heart containing Coding Chica Java 101

What’s Yours is Mine! Inheritance in Java

TIP: References Quick List

Table of Contents

  1. Table of Contents
  2. Introduction
  3. Abstract / Interface Methods
    1. get
  4. Inherited Methods with Bodies (Concrete)
    1. toString
  5. Java Syntax
  6. Summary
  7. The Diagram

Introduction

One of the benefits an object-oriented language, such as Java is inheritance. When a parent class defines a method that is accessible to the calling code, then any uses of the child class also have the ability to invoke that logic, with no action or update within the child class.

Abstract / Interface Methods

Those methods in the diagram below that are shown in italics are abstract, meaning that there is no method logic (body) defined. Any classes that extends an abstract class or implements an interface with unimplemented method(s) needs to be able to either:

  • Implement all of the missing method bodies, or
  • Call itself abstract, in which case, instances of that class cannot be created directly. Instead, a child class that implements all of the remaining missing method bodies could be created.

For example, the public E get(int index) method in ArrayList<E> is an abstract method defined in the List<E> interface and not implemented in the AbstractList<E> parent class. Therefore, the method body was defined in the List<E> class. Here, we can tell that AbstractList<E> did not specify the method logic, as it shows up in the Specified by section of the method’s Javadoc.

get

public E get(int index)

Returns the element at the specified position in this list.

Specified by:
get in interface List<E>

Specified by:
get in class AbstractList<E>

Parameters:
index – index of the element to return

Returns:
the element at the specified position in this list

Throws:
IndexOutOfBoundsException – if the index is out of range (index < 0 || index >= size())

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#get(int)

Inherited Methods with Bodies (Concrete)

If one of the parent class(es) define the method body logic (implementation) for a method, then, if no action is taken by the child class, then that implementation will be inherited and used by that class. This is called inheritance. A child could choose another path, but let’s save that for another discussion.

Looking at the AbstractCollection<E> Javadoc’s section for Concrete Methods section, we can see that toString() was implemented in that class.

toString

public String toString()

Returns a string representation of this collection. The string representation consists of a list of the collection’s elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

Returns:
a string representation of this collection

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/AbstractCollection.html#toString()

It doesn’t matter for the purposes of this discussion whether any of the other parent classes (those without the <<interface>> decoration on the diagram) also provided an implementation. It has been implemented in at least one of the parents in the hierarchy. Therefore, that logic, is available to the child class – ArrayList – if desired.

We will talk about the other option (not using the method logic inherited from the parent(s) – AKA overriding) in another post.

Java Syntax

Ignoring the other classes and interfaces not part of the diagram below, we can specify that a class realizes an interface with syntax like:

    public class MyClassName implements SomeInterfaceName {
    ...

If we want to implement more than one interface, we can do so separating them with a comma and a space.

    public class MyClassName implements SomeInterfaceName, SomeOtherInterfaceName {
    ...

Here, we don’t need to specify Object as the parent. It is assumed that Object is the parent if no other parent is specified.

If we want to specify the parent class, so we inherit from another class besides just Object, we can specify it like:

    public class MyClassName extends MyParentClassName {
    ...

In Java, a class can only have one parent – either Object or some explicitly mentioned class name.

Summary

When inheriting from a parent class, a child class receives all of the visible, implemented methods from the parent with no additional work. The child class may choose to continue with those method implementations or to write their own, instead. However, that is a topic for another post.

The Diagram

What is seen at the bottom of this page is a UML Class Diagram. It was manually created from the Java 17 Javadocs using the Mermaid Diagrams Live Editor. It’s definitely not perfect, especially since it’s a manual translation. However, what I wanted to make sure you could visualize the inheritance of methods. When following arrows to the left the diagram, if a method like toString() is implemented in a parent, then it need not be re-implemented in a child class (those toward the right)…it can just be inherited directly.

  • The arrows displayed with a dashed line are realizations (implementations) of an interface.
  • The arrows displayed with a solid line are inheritance (is-a) relationships for objects, where the arrow points at the parent.

We will talk more about UML diagrams in a future post. However, here are some links if you want to learn more about UML class diagrams in general or how to define them in Mermaid diagram notation. I put this at the bottom of the page, to reduce scrolling for those that do not want the visualization.

A diagram showing the public methods inherited and defined by the java.util.ArrayList class. See https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html as an alternative.
java.util.ArrayList class diagram

What’s Yours is Mine! Inheritance in Java

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.