Java · Intermediate level

Intermediate level · Module 1 of 6 · 15 guided hours

Inheritance, Interfaces and Polymorphism

Reuse and extend code with inheritance, define contracts with interfaces and abstract classes, and write code that works with many types through polymorphism.

Learning objectives

  • Extend classes and override methods correctly
  • Choose between abstract classes and interfaces
  • Use polymorphism to write general, extensible code

Topics

  • extends and super
  • method overriding and @Override
  • abstract classes
  • interfaces and default methods
  • polymorphism and dynamic dispatch
  • composition over inheritance

Lessons

  1. Lesson 1 Inheritance and Overriding

    Create subclasses that reuse a parent's fields and methods, call the parent's constructor with super, and override behaviour.

    superclass and subclasssuper(...)@Overrideprotected

  2. Lesson 2 Abstract Classes and Interfaces

    Define shared contracts with interfaces, share partial implementations with abstract classes, and choose between them.

    interfaceimplementsabstract methoddefault method

  3. Lesson 3 Polymorphism and Composition

    Write code against interfaces so it works with any implementation, and prefer composition when inheritance does not fit.

    polymorphismprogram to an interfaceinstanceof and pattern matchingcomposition

Practical activity

Model the payment methods of a shop (cash, mobile money, bank transfer) behind one PaymentMethod interface, and process a list of mixed payments with one loop.

Expected outcomes

  • Class designs that avoid duplicated code
  • Programs that accept new types without changing existing logic

Assessment

Design exercise: extend a given hierarchy with a new type without modifying the processing code; reviewed for correct overriding and use of the interface.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.