Command Palette

Search for a command to run...

Phase 5Intermediate6 of 17 in Core Java

Inheritance and Polymorphism

extends, super, overriding, dynamic dispatch, abstract classes, interfaces, sealed types, final, composition and nested classes, down to the bytecode.

Phase 4 taught you to build one class at a time. Real programs have families of classes: a Dog and a Cat that are both animals, a card payment and a UPI payment that are both payments. This phase is about how Java lets classes share behaviour (inheritance), stand in for each other (polymorphism), and promise behaviour (abstract classes and interfaces).

Every topic goes from an everyday picture to the real mechanism: what the compiler checks, which bytecode instruction it emits (invokevirtual, invokeinterface, invokespecial), how the JVM finds the right method at run time through vtables, and how the JIT makes that fast. You will also meet the modern tools: default methods (Java 8), private interface methods (Java 9) and sealed classes (Java 17).

The phase ends with the design judgement that interviews test: when inheritance is the wrong tool and composition is better, and how nested and anonymous classes work under the hood. Where an idea is really a design pattern, we link to the System Design course's pattern pages so you can see it used at scale.

0/13 · 0%
13 topics ~7 h 45 code blocks & diagrams
Start with the first topic
1
5.1

Inheritance with extends

class Dog extends Animal makes Dog a kind of Animal: it gets Animal's fields and methods for free and can add its own. A class can extend only one class, and every class ultimately extends java.lang.Object.

30 min 3 code practice

2
5.2

The super Keyword

super lets a subclass reach its parent: super(...) calls a parent constructor, super.method() runs the parent's version of an overridden method, and super.field reads a parent field that the child has hidden.

29 min 4 code practice

3
5.3

Method Overriding and @Override

Overriding means a subclass gives its own version of an inherited instance method with the same name and parameters. @Override asks the compiler to check that you really are overriding, which catches typos and wrong signatures.

29 min 4 code practice

4
5.4

Polymorphism and Dynamic Dispatch

Polymorphism means one variable type can hold objects of many classes, and a method call runs the version that belongs to the actual object. The JVM does this with dynamic dispatch, using per-class method tables (vtables) that the JIT then optimises away when it can.

31 min 3 code practice

5
5.5

Upcasting, Downcasting and instanceof

Upcasting (child to parent type) is automatic and always safe. Downcasting (parent to child type) needs an explicit cast, is checked at run time, and throws ClassCastException if the object isn't really that type, so you test first with instanceof.

29 min 3 code practice

6
5.6

Abstract Classes

An abstract class is a partly finished class: it can hold fields, constructors and working methods, plus abstract methods with no body that every concrete subclass must fill in. You can't create an object of an abstract class directly.

29 min 3 code practice

7
5.7

Interfaces

An interface is a named list of abilities, a contract, that any class can promise to provide with implements. A class can implement many interfaces, and code written against an interface works with every class that implements it.

27 min 3 code practice

8
5.8

Default, Static and Private Interface Methods

Since Java 8 an interface can contain default methods (with a body that implementers inherit) and static helper methods; since Java 9 it can also have private methods shared by those bodies. Default methods let old interfaces grow without breaking existing classes.

31 min 4 code practice

9
5.9

Abstract Class vs Interface

Use an interface to define what a type can do, especially when unrelated classes share a capability. Use an abstract class when closely related classes share state and code. Often the best design is both: an interface for the contract and an abstract class that implements most of it.

27 min 3 code practice

10
5.10

The final Keyword

final means 'can't be changed after it's set': a final variable can be assigned only once, a final method can't be overridden, and a final class can't be extended. A final reference can't point elsewhere, but the object it points to can still change.

28 min 3 code practice

11
5.11

Sealed Classes and Interfaces

A sealed class or interface lists exactly which classes may extend or implement it, with permits. The family is closed: the compiler and JVM reject any other subclass, and (from Java 21) a switch over a sealed type can be checked for completeness without a default.

31 min 3 code practice

12
5.12

Composition over Inheritance

Composition means building a class by holding other objects in fields and delegating work to them (has-a), instead of inheriting from them (is-a). It avoids the fragile base class problem and lets behaviour change at run time, so prefer it unless the relationship is a true, stable is-a.

30 min 4 code practice

13
5.13

Nested, Inner, Local and Anonymous Classes

Java lets you declare a class inside another class, or even inside a method. A static nested class is an ordinary class that lives in another's namespace; an inner class is tied to one outer object through a hidden reference; local and anonymous classes live inside a block and can read effectively final local variables. Since Java 8, a lambda replaces most anonymous classes that implement a one-method interface.

48 min 5 code practice