E ExamMaster

Object-Oriented Programming through Java · Arrays and Inheritance

Inheritance

In Object-Oriented Programming through Java because a student ticket is still a ticket — reuse, super, overriding, dynamic dispatch and abstract moulds are how Java writes that.

StudentTicket is a Ticket with one extra rule. Inheritance says the child is a kind of the parent. Java allows one parent class. Object sits at the top. super reaches the parent. Overriding lets the child's method run even when the variable is typed as Ticket. abstract means the mould is incomplete.

  • Object-Oriented Programming through Java
  • Easy level
  • 7 concepts

1A child class is a kind of the parent

Inheritance is the sentence StudentTicket is a Ticket. The child reuses the parent's fields and methods and may add its own.

Write extends Ticket. You do not copy-paste price and seat. You say the new class is a kind of the old one.

Figure. The arrow is is-a, from child to parent. It is not has-a. A ticket is not a kind of student.

How it works

  1. A child class is a kind of the parentextends means is-a. The child reuses the parent; it does not own the parent.

StudentTicket is a Ticket

public class StudentTicket extends Ticket {
    private String college;
}

2Java's trees: single, multilevel, hierarchical

Single inheritance is one parent. Multilevel is a chain: WeekendStudentTicket extends StudentTicket extends Ticket. Hierarchical is two children of one parent: StudentTicket and StaffTicket.

Java will not let a class extend two classes. That missing shape is why interfaces exist in the next lesson.

Figure. Ticket has two children (hierarchical). Student has one child (multilevel). No class has two parents.

How it works

  1. Java's trees: single, multilevel, hierarchicalOne parent class. Chains and forks are fine. Two parents are not.

3Object is the parent you get for free

If Ticket writes no extends, it still extends Object. equals, hashCode and toString live there.

A variable of type Object may hold any ticket. That is useful for a mixed box; it is a poor type for the desk, which needs seat and price.

Figure. Ticket writes no extends, so the parent is still Object. equals, hashCode and toString live there until Ticket overrides them.

How it works

  1. Object is the parent you get for freeEvery class is an Object. Override toString when the default heap address is not what the desk should print.

toString comes from Object unless Ticket overrides it

Ticket t = new Ticket("A12", 200);
System.out.println(t.toString());
Object box = t;

4super reaches the parent; constructors chain

super.priceAfterTax() calls the parent's method. super(seat, price) calls the parent's constructor.

A child constructor must chain to a parent constructor, explicitly with super(...) or by Java inserting super(). If Ticket only has Ticket(String, int), StudentTicket must call that form. The super(...) call is the first statement.

Figure. The child constructor calls super(seat, price) as its first statement, Ticket is built, then this.college is set. Reverse that order and it will not compile.

How it works

  1. super reaches the parent; constructors chainsuper(...) is first. If the parent has no no-arg constructor, you must write the matching super call.

Child constructor chains first

public StudentTicket(String seat, int price,
                    String college) {
    super(seat, price);
    this.college = college;
}

5Overriding uses the object's class at run time

Overriding is a child method with the same signature as the parent. @Override asks the compiler to check that.

Dynamic dispatch means the call uses the object's class, not the variable's type. Ticket t = new StudentTicket(...); then t.priceAfterTax() runs the student method.

Figure. The variable says Ticket. The object is a StudentTicket. The method that runs is the child's.

Same signature, student rule

@Override
public int priceAfterTax() {
    return super.priceAfterTax() / 2;
}

Dispatch on a student ticket

Ticket.price() returns 200. StudentTicket overrides it to return 150. Ticket t = new StudentTicket(); What does t.price() return?

  • variable typeTicket
  • object classStudentTicket
  • t.price()150

Pro tip. If dispatch used the variable type, the answer would be 200. It uses the object.

Coding lab. Dispatch prints the student fare runs in the app, with checks on your output.

6abstract means the mould is incomplete

An abstract class may hold abstract methods — names with no body. You cannot write new Pass(). A child must fill the missing methods or stay abstract.

Use it when several tickets share fields and some work, but the last step differs and must not be forgotten.

Figure. Pass is an incomplete mould: new Pass() is refused. A child must write priceAfterTax() or stay abstract.

How it works

  1. abstract means the mould is incompleteabstract class: no new. abstract method: a child must write the body.

Pass cannot be constructed

public abstract class Pass {
    protected int price;
    public abstract int priceAfterTax();
}

7final closes the child list or the method

A final class cannot be extended. String is the standard example; your Ticket can be final if the desk must not grow secret subclasses.

A final method cannot be overridden. The parent's body is the last word. That is stronger than just omitting @Override — the compiler refuses the child's copy.

Figure. A final class cannot be extended. A final method cannot be overridden — the compiler refuses the child's copy of seatCode.

How it works

  1. final closes the child list or the methodfinal class: no children. final method: no override.

seatCode cannot be overridden

public class Ticket {
    public final String seatCode() {
        return seat;
    }
}

Notes

  • In Object-Oriented Programming through Java because a student ticket is still a ticket — reuse, super, overriding, dynamic dispatch and abstract moulds are how Java writes that.
  • Java has single class inheritance, plus multilevel and hierarchical trees. There is no multiple class inheritance.
  • Every class extends Object if you write no parent. super() chains constructors. Overriding uses the object's class at run time.

Exam traps & shortcuts

  • extends names one class. A second parent class is a compile error — use an interface for extra contracts.
  • Ticket t = new StudentTicket(...); then t.priceAfterTax() runs the student method if it overrides. That is dispatch.

Reference tables

These are trees of classes. Multiple inheritance of classes is not in the language; two parents is an interfaces lesson.

Inheritance shapes Java actually has
ShapeWhat it looks like at the desk
SingleStudentTicket extends Ticket
MultilevelWeekendStudentTicket extends StudentTicket
HierarchicalStudentTicket and StaffTicket both extend Ticket
Multiple classesNot legal. Two interfaces are legal.

Recap

A child is a kind of the parent. One parent class. super chains. The object picks the override. abstract is incomplete; final is closed.

is-a, one parent
extends one class. Two class parents are illegal.
Object is the top
toString and equals live there until you override them.
dispatch uses the object
Ticket t holding a StudentTicket still runs the student method.
abstract vs final
abstract must be finished. final must not be changed.

Practise Inheritance

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.