Skip to content
Object-Oriented Programming and Design · Term 2

Polymorphism and Interfaces

Utilize interfaces and abstract classes to define common behaviors across different object types.

Need a lesson plan for Computer Science?

Generate Mission

Key Questions

  1. How does polymorphism allow a system to be extended without modifying existing code?
  2. What is the difference between an interface and an abstract class in system design?
  3. How do standardized interfaces facilitate collaboration between different development teams?

Ontario Curriculum Expectations

CS.HS.P.4CS.HS.D.1
Grade: Grade 11
Subject: Computer Science
Unit: Object-Oriented Programming and Design
Period: Term 2

About This Topic

Polymorphism enables different classes to share common behaviors through interfaces or abstract classes, allowing code to handle objects uniformly without knowing their exact types. Grade 11 students implement interfaces, which define method signatures only, and abstract classes, which mix abstract methods with concrete ones. For example, a Drawable interface lets Circle, Rectangle, and Triangle classes override draw() for varied outputs, connecting to Ontario's emphasis on object-oriented design.

This topic supports the Open-Closed Principle, where systems extend without changing existing code, and highlights interfaces for team collaboration via standardized contracts. Students compare interfaces, simulating multiple inheritance, with abstract classes for code reuse. These concepts build skills in modular programming and system scalability, preparing for advanced software development.

Active learning benefits polymorphism through pair refactoring and group simulations. When students extend shared codebases or contract-test interfaces across teams, abstract ideas become concrete. Collaborative debugging reveals runtime polymorphism, fostering deeper understanding and confidence in design choices.

Learning Objectives

  • Compare the implementation of interfaces versus abstract classes in defining common behaviors for a set of related objects.
  • Analyze how polymorphism, through interfaces and abstract classes, supports the Open-Closed Principle in software design.
  • Create a simple class hierarchy that utilizes an interface to enforce a common method signature across different object types.
  • Explain how standardized interfaces facilitate collaboration and contract-based development between software engineering teams.
  • Evaluate the trade-offs between using interfaces and abstract classes for code reuse and extensibility in a given scenario.

Before You Start

Classes and Objects

Why: Students must understand the fundamental concepts of classes as blueprints and objects as instances to grasp how interfaces and abstract classes relate to them.

Inheritance

Why: Understanding how classes can inherit properties and behaviors from parent classes is crucial for comprehending how abstract classes and interfaces extend or define common functionality.

Method Signatures and Implementation

Why: Students need to know what a method signature is and how it differs from a method's implementation to understand the core distinction between interfaces and abstract classes.

Key Vocabulary

InterfaceA contract that defines a set of method signatures without implementation. Classes that implement an interface must provide concrete implementations for all its methods.
Abstract ClassA class that cannot be instantiated directly. It can contain both abstract methods (without implementation) and concrete methods (with implementation), allowing for code reuse and extension.
PolymorphismThe ability of an object to take on many forms. In object-oriented programming, it allows objects of different classes to be treated as objects of a common superclass or interface.
Abstract MethodA method declared in an abstract class or interface that has no body or implementation. Subclasses or implementing classes must provide the implementation.
Open-Closed PrincipleA design principle stating that software entities (classes, modules, functions) should be open for extension but closed for modification.

Active Learning Ideas

See all activities

Real-World Connections

Game development studios use interfaces to define common actions for different types of characters or game objects, such as `IAttackable` or `IDamageable`. This allows the game engine to interact with various entities uniformly, even if their underlying implementations differ.

Financial software platforms utilize standardized interfaces for payment processing. Different payment gateways (e.g., credit card, PayPal, bank transfer) can implement a common `IPaymentGateway` interface, enabling the system to seamlessly switch or add new payment methods without altering core transaction logic.

Watch Out for These Misconceptions

Common MisconceptionInterfaces and abstract classes function identically.

What to Teach Instead

Interfaces enforce pure contracts without implementations; abstract classes allow shared concrete code. Side-by-side coding in pairs lets students experiment with both, seeing how partial implementations in abstract classes reduce duplication while interfaces promote flexibility.

Common MisconceptionPolymorphism changes object types at runtime.

What to Teach Instead

Polymorphism treats subclasses as the parent type via references, calling overridden methods dynamically. Group simulations with mixed object lists clarify this through observed behaviors, helping students distinguish reference types from actual objects.

Common MisconceptionYou can create instances of interfaces or abstract classes.

What to Teach Instead

These cannot be instantiated; they serve as blueprints. Hands-on attempts followed by error discussions in small groups reinforce the need for concrete subclasses, building correct mental models through trial and correction.

Assessment Ideas

Quick Check

Present students with two code snippets: one using an interface and one using an abstract class to achieve similar functionality. Ask them to identify which is which and write one sentence explaining the primary difference in their approach.

Discussion Prompt

Pose the question: 'Imagine you are designing a system for managing different types of vehicles (cars, bikes, trucks). How would you use interfaces and abstract classes to ensure all vehicles can be `started` and `stopped`, but in unique ways?' Facilitate a class discussion on their proposed solutions.

Exit Ticket

Provide students with a scenario: 'A new `Robot` class needs to be added to an existing system that already has `Dog` and `Cat` classes, all of which can `makeSound()`. The `makeSound()` method is implemented differently for each. Which concept, interface or abstract class, would be more appropriate for `makeSound()`, and why?'

Ready to teach this topic?

Generate a complete, classroom-ready active learning mission in seconds.

Generate a Custom Mission

Frequently Asked Questions

What is the difference between an interface and an abstract class?
Interfaces declare only abstract methods and constants, supporting multiple inheritance-like behavior for contracts across unrelated classes. Abstract classes can include concrete methods, constructors, and fields for shared code in hierarchies. Use interfaces for broad compatibility, abstract classes for related classes needing common functionality. This distinction aids flexible design in team projects.
How does polymorphism support extending code without modifications?
Polymorphism follows the Open-Closed Principle: new subclasses implement interfaces or extend abstract classes, adding behaviors via overrides without altering existing code. A list of base-type references handles new types seamlessly. Students see this in refactoring exercises, where adding a new shape works instantly in polymorphic loops, demonstrating maintainable systems.
What are real-world examples of polymorphism and interfaces?
USB interfaces let diverse devices plug into hosts polymorphically. GUI frameworks use Drawable interfaces for buttons, icons, and panels. Payment systems process CreditCard or PayPal via a Payment interface. These show how standardized contracts enable plug-and-play extensions, mirroring software modularity students build in class projects.
What active learning strategies teach polymorphism effectively?
Use pair programming for implementing interfaces, small-group plugin simulations, and whole-class refactoring challenges. Students extend shared code without breaks, testing polymorphic arrays live. Peer reviews and error-sharing sessions connect theory to practice, making runtime method dispatch tangible and boosting retention through collaboration.