4.23 Lecture 8
Introduction to Systems Programming - Lectures - Class 8.pdf
Extending classes
In Java, as in most OOP languages, any class will be a subclass of another. In Java we have a root class which is the exception of this rule. Object is not a subclass of any other class, but all other classes are a subclass of Object.
Any class A that is a subclass of B will have access to:
- Public fields, methods, and constructors.
- Package fields, methods, and constructors.
- Protected fields, methods, and constructors.
Any class A that is a subclass of B can redefine/overwrite:
- Methods, but access permissions must remain equal or greater.
E.g.: a protected method in B can be made protected or public in A.
If class B is a subclass of class A, then B can be used in place of A. The opposite is not possible.
Continuing from the previous example, any constructor of B must call a constructor of A as its first statement. This rule might not always be visible because of empty constructors, if A has an empty constructor, then it will be used by default if a constructor in B doesn’t call it explicitly.
Superclass constructor call
- Subclass constructors must always contain a 'super' call.
- If none is written, the compiler inserts one (without parameters).
works only, if the superclass has a constructor without parameters - Must be the first statement in the subclass constructor
Extending classes
“B extends A” can be seen as “B is an A”.
Inheritance also applies to interfaces:
public class LinkedList<T> implements List<T>
Also means “LinkedList is a List”
Extending classes in Java-What can we extend?
- Interfaces (extending an interface is not the same as implementing one!).
- Classes.
- Abstract classes.
An abstract class in a partially implemented class, it cannot have instances, we have seen an example before in class Figure.
Subclasses and subtyping
- Classes define types.
- Subclasses define subtypes.
- Objects of subclasses can be used where objects of supertypes are required.
This is called substitution
Subclasses and parameter passing
public void addArea(Figure figure)
{
this.area += figure.area();
}
Figure circle = new Circle(3.5f);
Figure rectangle = new Rectangle(4.0f, 2.0f);
Square square = new Square(2.0f);
addArea(circle);
addArea(rectangle);
addArea(square);
A question
If a variable of class/type A can be assigned any value of type A or a subclass of A; what types (non-primitive) can be assigned to an Object variable?
Can be assigned a value of any non-primitive type.
Inheritance hierarchy
Possible solutions
- Redefine method display on each Post subtype/subclass.
- Make display abstract in Post, so it must be defined in each subclass.
- Define method display as:
public void display()
{
//show author
//call displayBody()
//show timestamp and votes
}
To discuss
Given class A with methods m1, m2, and m3; classes B and C as subclasses of A overriding methods m1, and m2; and class D as a subclass of B overriding method m2.
Given the statements
A x = randomlnstanceOfA();
x.m2();
x.m3();
With method randomlnstanceOfA
giving an instance of A, B, C, or D. How do we know which method "m2" and "m3" is called?
- For x.m2(), you cannot know which specific method is called without knowing the runtime type of the object. It depends on whether x holds an instance of A, B, C, or D.
- For x.m3(), you know exactly which method is called. Since m3() is not overridden in any subclass, the original implementation from class A is always executed, regardless of the runtime type of x