Inheritance Basics:
Basic Definitions:
- Superclass/Parent Class A class that other classes are derived from
- Subclass/Child Class: A class that is derived from another class
- Inheritance: When a child class inherits the state and behaviors of a parent class
Benefits/Uses:
- Makes creating multiple classes that share similar states/behaviors easier
- Conforms to DRY Principle
- Makes debugging issues easier
- Increases modularity
Class Hierarchies:
- Represents the passing of state and behavior to other classes
- The Java standard library called Object is at the top of every Inheritance hierarchy (not show in diagram below)
- Multiple Inheritance is illegal
- A superclass can have infinite subclasses but a subclass can only have one superclass
- A class can be a superclass and a subclass at the same time
The Protected and Default Modifiers:
Protected:
- In between public and private
- Like private but allows access from a subclass and package
Default:
- When no modifier is added
- In between protected and private
- Allows access from the class and same package (not different ones)
Subclasses:
Declaring Subclasses:
To declare a subclass you use an extends clause in the header of the class
//superclass
public class sClass {
}
//subclass (would be in different file)
public class bClass extends sClass {
}
Subclass Constructors and the super() method:
A subclass constructor will auto call a superclass constructor on the first line without parameters if super() isn't used. If the superclass doesn't have a parameter-less constructor, this will lead to a compile error.
- The super() method can be used to call the superclass constructor
- Allows us to create/use superclass variables for the subclass
//Format
super(parameters); //Calling constructor
Inheriting and Overriding Methods with super():
- The super() method also allows us to call methods from the superclass
- To override a method, create a method with the same signature in the subclass
- The signature must be the exact same
- A subclass can override the visiblity of a method, but a superclass can’t
//Format
public returnType methodName(parameters) {
super().methodName(parameters); //Calling the method in the superclass
}
Inheritance and the final Keyword:
This is in refrence to using final with a method or class. Click here to see how final is used with variables.
- The final keyword prevents a method being changed/overridden by any subclasses
- Should be used to ensure that a specific/purpose-built method stays the same throughout the entire method hierarchy
- The final keyword prevents a class from being a superclass (prevents a class from being extended).
- Can still be a subclass
//Format for a Method
public final returnType methodName(parameters) {
}
//Format for a Class
public final class className {
}
The Abstract Modifier vs Concrete:
- Concrete is used to define non-abstract methods/classes
- AKA ‘normal’ methods/classes
- The abstract modifier is used in the same way as the final modifier
- Abstract classes can’t be instantiated
- can’t use ‘new’ keyword to create an instance
- Should represent generic concepts
- Methods that use the abstract Modifier need to be defined by the subclass, not the superclass
//superclass
public abstract class Greeting {
public abstract void test();
}
//Subclass
public class hello extends Greeting {
public void test() {
System.out.println("hi");
}
}
The Object Class and Overriding equals:
- The object class has an equals method that can be called on any object by default
- String version of equals overrides this method
- Checks if two objects are equal to each other (checks if they are aliases)
- Must check that the object is not null and that it is an instance of the current class
//Format
public boolean equals(Object o) {
if (!(o instanceof className)) {
return false
}
className other = (className) o;
return equalsCheck;
}