Created: 2023-08-25 20:34
Status: #concept
Subject: Programming
Tags: Java Object-Oriented Programming Prototypal Inheritance Polymorphism Java Interface

Inheritance

When a parent (superclass) shares Properties and Methods with a child (subclass), the child "inherits" all of the parent's fields and can use them.

  • according to the Liskov Substitution Principle, the child must be able to act as the parent, enabling Polymorphism.
  • it is a tool for building and specializing hierarchies of concepts; a subclass is always a special case of the superclass.

If the subclass doesn't need or use some of the inherited functionality, inheritance is NOT justifiable.

Java

In Java or JavaScript, we can let a class NewClass extends AnotherClassName to let NewClass inherit the fields of AnotherClassName.

  • if we want to inherit a Constructor function, we need to call the super() function while passing the arguments to build the parent's constructor.
  • we cannot use the this keyword without doing super(...args) on a child.

Unlike Python, we cannot inherit from multiple parents, only 1.

public class Person {
    private String name;
    private String address;

    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }
    
    @Override
    public String toString() {
        return String.format("%s\n  %s", this.name, this.address);
    }
}

public class Student extends Person {
    private int credits;
    
    public Student(String name, String address) {
        super(name, address);
        this.credits = 0;
    }
    
    public void study() {
        this.credits++;
    }
    
    public int credits() {
        return this.credits;
    }
    
    @Override
    public String toString() {
        return super.toString() + String.format("\n  Study credits %d", this.credits); 
    }
}

Calling Methods & Attributes from a Superclass

We have to use the super scope instead of this in order to call any methods inherited from the super class.

  • we can only access public or protected methods or attributes in this case, while private properties are only accessible by the parent's class itself.

@Override
public String toString() {
    return super.toString() + "\n  And let's add my own message to it!";
}

Declaring Parent Instances with Subclass Initialization

An object's type decides what methods are available to use.

For example, if we have a Student extends Person, but declare Person p = new Student("John", "BSCS"), we cannot use student-specific methods like .study().

  • however, when we override a Person method like toString(), the Student's implementation is used, see Polymorphism.
  • if no usable method is available in the subclass, we check the nearest parent & so on, until we have null.
Person ollie = new Student("Ollie", "6381 Hollywood Blvd. Los Angeles 90028");
ollie.credits();        // DOESN'T WORK!
ollie.study();              // DOESN'T WORK!
System.out.println(ollie);   // ollie.toString() WORKS

References