Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 39 ยท Class and Object Diagrams

Inheritance, Abstract Classes and Interfaces

Model is-a relationships with UML generalization, show abstract classes and interfaces, use realization and polymorphism, and learn when composition beats inheritance.

Associations say "has a link with". Generalization says "is a kind of". It lets you pull shared structure and behavior into a parent class so that child classes only state what is different. UML also separates what something promises (an interface) from how it does it (a class that realizes the interface). This lesson covers both.

Generalization

Generalization is drawn as a solid line with a hollow triangle pointing at the parent (the more general class).

classDiagram
    class Vehicle {
        #String plate
        +start()
        +stop()
    }
    class Car {
        +int doors
    }
    class Truck {
        +float loadCapacity
    }
    Vehicle <|-- Car
    Vehicle <|-- Truck

Car and Truck inherit plate, start and stop from Vehicle and add their own members.

The test is the is-a sentence: "A Car is a Vehicle" reads well. "A Car is an Engine" does not; that is a part-of relationship (see the next lesson).

What children get from the parent:

  • Attributes and operations (subject to visibility; private members are not accessible).
  • The right to be used wherever the parent is expected (substitutability).
  • Relationships the parent has with other classes.

A child may override an operation to change its behavior. Show the overriding operation again in the child box.

Abstract classes

An abstract class is not meant to be instantiated directly; it exists to be specialized. Its name is shown in italics or with the constraint. It may contain abstract operations (no body) and concrete ones.

classDiagram
    class Shape {
        <<abstract>>
        +area() double*
        +describe() String
    }
    class Circle {
        -double radius
        +area() double
    }
    class Rectangle {
        -double width
        -double height
        +area() double
    }
    Shape <|-- Circle
    Shape <|-- Rectangle

Shape cannot be created; each subclass must supply area().

Interfaces and realization

An interface is a contract: a set of operations with no implementation and (normally) no attributes. A class that realizes (implements) it promises to provide those operations. Realization is a dashed line with a hollow triangle. Interfaces carry the stereotype <<interface>>.

classDiagram
    class Payable {
        <<interface>>
        +pay(float amount) bool
    }
    class CreditCard {
        +pay(float amount) bool
    }
    class PayPalAccount {
        +pay(float amount) bool
    }
    Payable <|.. CreditCard
    Payable <|.. PayPalAccount

Code that depends on Payable works with either implementation.

A class can realize many interfaces but, in most languages, inherit from only one parent class. That is why interfaces are the main tool for flexible designs.

Generalization Realization
Line solid, hollow triangle dashed, hollow triangle
Meaning is a kind of promises to provide
Brings implementation yes no (only the contract)
Typical count one parent many interfaces

Polymorphism

Inheritance and interfaces together enable polymorphism: code written against the parent or interface works with any child, each responding in its own way.

Python Polymorphism through a shared parent

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python An interface as a Protocol

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The lollipop notation

A compact way to show interfaces: a small circle on a stick (the provided interface) attached to the class that realizes it, and a half circle (the required interface, "socket") on a class that needs it. Lollipops appear mostly in component diagrams (Chapter 5), but you may meet them in class diagrams too.

Multiple inheritance

UML allows a class to have several parents. Some languages (C++, Python) support it directly; others (Java, C#) allow it only for interfaces. Multiple inheritance of implementation invites the "diamond problem", where two parents provide the same member. Prefer interfaces when you can.

Inheritance or composition?

Inheritance is the strongest coupling in object design: a child depends on its parent's internals. Use this checklist before drawing a triangle:

  • Is it truly is-a in all situations, forever? (A Stack is not an ArrayList, even though it can be built on one.)
  • Will the child honor the parent's contract everywhere (Liskov substitution)? A Penguin that throws in fly() breaks Bird.
  • Is the relationship about behavior that varies instead? Then give the class a collaborator (composition) and keep the hierarchy shallow.

Choosing the relationship

Generalization Is a kind of. Share structure.
Realization Promises a contract.
Composition Has a part. Next lesson.

Hierarchies deeper than three levels deserve a second look.

1 / 2

Common mistakes

  • Reusing code via inheritance. If the only reason is sharing methods, use composition or a helper.
  • Wrong arrow direction. The triangle always points at the parent or interface.
  • Solid line to an interface. Realization is dashed.
  • Repeating inherited members. Show them in the child only when overriding.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which line style shows a class implementing an interface?

    Realization is dashed with a hollow triangle at the interface; solid with a triangle is generalization.
  2. 2. Where does the triangle point in generalization?

    The triangle touches the more general class.
  3. 3. Which statement best describes an abstract class?

    Abstract classes provide shared structure and sometimes abstract operations for subclasses.
  4. 4. Why is a Penguin extending Bird (with fly) a design smell?

    Subclasses must honor the parent's contract; otherwise the hierarchy is wrong.

Summary

  • Generalization (solid line, hollow triangle at the parent) models is-a.
  • Abstract classes are italic and cannot be instantiated; abstract operations are italic too.
  • Interfaces (<<interface>>) define contracts; realization is a dashed line with a hollow triangle.
  • Polymorphism lets callers depend on the parent or interface.
  • Prefer composition and interfaces when behavior varies; avoid deep hierarchies.

Next: Aggregation, Composition and Dependency