Module 6 · Inheritance, Polymorphism and Interfaces — topic 3 of 3
Abstract Classes and Interfaces
Abstract classes with partial implementations, interfaces as contracts, default and static interface methods, implementing several interfaces, functional interfaces and sealed types.
Learning objectives
- Write abstract classes with abstract and concrete methods
- Define and implement interfaces, including default methods
- Choose between an abstract class and an interface
Definition
Abstraction exposes what an object does while hiding how. An abstract class cannot be instantiated and may contain abstract methods for subclasses to implement; an interface declares a contract of methods that any class can implement, regardless of its superclass.
Introduction
Some types are too general to exist on their own. There's no such thing as 'just a Shape' — every real shape is a circle, a square or something else. Abstract classes and interfaces let you describe what all shapes can do while leaving the details to concrete classes.
Why it matters
Interfaces are everywhere in Java: List, Comparable, Runnable, AutoCloseable. Writing code against interfaces makes it flexible and testable.
Knowing when to use an abstract class versus an interface is a classic design and interview question.
Detailed explanation
Abstract classes. Declared with abstract class. They can't be created with new, but they can have fields, constructors, concrete methods and abstract methods (no body) that every concrete subclass must implement. Use them when related classes share state and code.
Interfaces declare methods that implementing classes promise to provide: class Circle implements Shape. Interface methods are implicitly public abstract; fields are implicitly public static final constants.
Several interfaces. A class extends at most one class but can implement many interfaces: class Duck extends Bird implements Flyer, Swimmer. This is how Java gets the benefits of multiple inheritance of type without its complications.
default and static methods (Java 8). An interface can provide default methods with a body, which implementing classes inherit and may override, and static helper methods. Java 9 also allows private interface methods for sharing code between defaults.
Functional interfaces have exactly one abstract method — Runnable, Comparator, and your own interfaces marked @FunctionalInterface. They can be implemented with lambdas (module 10).
Choosing. Use an interface to define a capability that unrelated classes may share (Comparable, Printable). Use an abstract class to share code and state among closely related classes. They combine well: an interface for the contract and an abstract class with a partial implementation.
sealed (Java 17). sealed interface Shape permits Circle, Square restricts which classes may implement a type, so the compiler knows every possibility — useful with switch pattern matching.
Syntax
- A class with an abstract method must itself be abstract.
- Implementing methods of an interface must be public.
- An interface can extend other interfaces.
Code examples
An abstract class
Circle: area 3.14, perimeter 6.28 Rect: area 6.00, perimeter 10.00 Circle: area 0.79, perimeter 3.14 Total area 9.93
Interfaces with default methods
Paid 1500 by card ending 4444 | fee 30 Paid 1500 via UPI asha@bank | fee 0
More examples
Several interfaces, a functional interface and a sealed type
[Mouse] Rs 600 [Desk] Rs 7500 [Laptop] Rs 55000 festive: 6750, flat: 7000, both: 6250 Car toll 80 Bike toll 20 Car toll 120
Step-by-step explanation
- Product implements two interfaces: Comparable (so
Collections.sortcan order products by price) and Labelled. Labelled.super.label()calls the interface's default method from inside the override, then adds the price.- Discount has one abstract method, so lambdas such as
price -> price * 90 / 100implement it; its defaultandThencomposes two discounts (7500 → 6750 → 6250). sealed interface Vehicle permits Car, Bikelists every allowed implementation;CarandBikeare records, which are implicitly final.- Because the compiler knows all Vehicle types, the pattern-matching switch in
tollneeds no default case.
Expected output
Running “Several interfaces, a functional interface and a sealed type” prints exactly:
Practical applications
- The Collections Framework: List, Set and Map are interfaces with several implementations
- JDBC: your code uses Connection and ResultSet interfaces implemented by each database driver
- Comparable and Comparator for sorting, Runnable and Callable for threads
- Abstract base classes such as AbstractList that make writing new collections easy
Best practices
- Declare variables and parameters with interface types (List, not ArrayList).
- Keep interfaces small and focused on one capability.
- Use an abstract class only when implementations really share state or code.
Common mistakes
Trying to instantiate an abstract type
Avoid
Prefer
Abstract classes and interfaces describe incomplete types; create a concrete subclass instead.
Reducing visibility of an interface method
Avoid
Prefer
Interface methods are public, and an implementation can't be less accessible than the method it implements.
Important notes
- If a class inherits the same default method from two interfaces, it must override it and may choose one with
InterfaceName.super.method(). - Sealed classes and interfaces became standard in Java 17; pattern matching for switch in Java 21.
Key takeaways
- Abstract classes can't be instantiated and may mix abstract and concrete methods.
- Interfaces define contracts; a class can implement many and inherit default methods.
- Functional interfaces have one abstract method and can be implemented with lambdas.
Try it yourself
- 1Define an interface
Taxablewithtax()and a defaultpriceWithTax(price), and implement it for two product types. - 2Create an abstract
Gameclass with a template methodplay()that calls abstractstart,turnandendsteps.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
9 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhy doesn't `Shape s = new Shape();` compile when Shape is abstract?
- BeginnerHow many interfaces can a class implement?
- BeginnerWhat is printed?