A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Abstract Classes and Interfaces

College Depth 92 in the knowledge graph ☐ I know this ☆ Set as goal
341prerequisites beneath it
See this on the map →
Inheritance and SubclassesMethod Overriding and Polymorphism+1 more→
abstract-classes interfaces OOP object-oriented abstraction contracts

Core Idea

An abstract class is a class that cannot be instantiated and may declare abstract methods, which have a name and parameters but no body. Every concrete subclass must supply bodies for those methods. An interface goes further: it names a set of methods that any class can promise to provide, without saying how. Both tools let code depend on what an object can do rather than on which exact class it belongs to.

How It's Best Learned

Start with a Shape superclass whose `area()` has no sensible default, and show that making it abstract stops anyone from creating a plain Shape. Then introduce a Java interface such as `Comparable` and show that unrelated classes (Student, Song, Date) can all be sorted by the same library method once they implement it. Finish by asking students to choose between an abstract class and an interface for a few small designs and to defend the choice.

Common Misconceptions

Explainer

From polymorphism you know that code like `shape.area()` runs the right version for each subclass. But what should `area()` do in the `Shape` class itself? There is no correct formula for a generic shape. Returning 0 would hide bugs. An abstract class solves this. In Java you write `public abstract class Shape` and declare `public abstract double area();` with a semicolon instead of a body. Two rules follow. No one can write `new Shape()`. And every concrete subclass, such as `Circle`, must provide a body for `area()`, or the compiler refuses it.

An abstract class is still a real class. It can hold fields such as `color`, a constructor that sets them, and finished methods such as `describe()` that call `area()`. Subclasses inherit all of that. The abstract class is a partly built template that the subclasses complete.

An interface is a pure promise. In Java, `interface Comparable<T> { int compareTo(T other); }` says that any class implementing it can compare itself to another object of that type. A `Student` class can write `implements Comparable<Student>`, define `compareTo`, and then the library method `Collections.sort` can sort a list of students. The sort method was written long before `Student` existed. It depends only on the interface. Since Java 8, interfaces may also contain default methods with bodies, but most interfaces are still mainly lists of required methods.

The key difference is what each one allows. A Java class extends at most one class, so an abstract class is the right choice for one family that shares state and code. A class can implement many interfaces, so interfaces fit capabilities that cut across families. A `Robot` and a `Dog` are not related, yet both might implement `SoundMaker`.

Python expresses the same ideas with the `abc` module. A class that inherits from `ABC` and marks a method with `@abstractmethod` cannot be instantiated until a subclass overrides that method; trying raises a `TypeError`. Python has no separate interface keyword, so abstract base classes with only abstract methods play that role.

Both tools serve the same design rule: write code against what an object can do, not against its exact class.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Inheritance and Subclasses → Method Overriding and Polymorphism → Abstract Classes and Interfaces

Longest path: 93 steps · 341 total prerequisite topics

Prerequisites (3)

Leads To (0)

No topics depend on this one yet.