Interfaces and Abstract Classes
# CHAPTER 16
Interfaces and Abstract Classes
1. Chapter Introduction
In Chapter 15, we learned that a class can only inherit from ONE parent class (Single Inheritance). But what if aSmartphone is both a Phone AND a Camera AND a GPS? To solve this, OOP relies on Interfaces and Abstract Classes. These allow us to define "Contracts" indicating what an object *should do*, without strictly defining *how* it should do it.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define and implement Interfaces.
- Understand how Interfaces bypass single-inheritance limits.
- Create Abstract Classes.
- Distinguish between when to use an Interface vs an Abstract Class.
3. What is an Interface?
An Interface is a contract. It defines a set of methods that a class MUST implement if it chooses to adopt the interface. Interfaces cannot hold state (properties with actual backing fields).4. Multiple Interfaces (Solving Single Inheritance)
While a class can only inherit from ONE parent class, it can implement an UNLIMITED number of Interfaces!*Because Interfaces only provide signatures, there is no confusion about which parent logic to use, making multiple implementation safe.*
5. Interfaces with Default Methods
In modern Kotlin, an Interface *can* actually provide a default implementation for a method. If a class implements the interface, it can choose to use the default or override it.6. What is an Abstract Class?
An Abstract Class is a hybrid between a standard Class and an Interface.-
Like an Interface: It cannot be instantiated (you can't do
val x = Animal()).
- Like a regular Class: It can hold state (actual variables).
Use it when you want a base class to provide *some* core logic and state, but force the child classes to fill in the missing pieces.
7. Interface vs Abstract Class Comparison
| Feature | Interface | Abstract Class |
|---|---|---|
| Instantiate objects? | No | No |
| Hold actual state (variables)? | No | Yes |
| Multiple implementation? | Yes | No (Single inheritance only) |
| Use Case: | "Can Do" (Behaviors like Clickable) | "Is A" (Core identity like Animal) |
8. Common Mistakes
-
Trying to instantiate an Interface or Abstract Class:
val s = Shape()will trigger a compiler error. They are blueprints for blueprints; they cannot exist as standalone objects.
-
Putting state in an Interface: Writing
var count: Int = 0inside an interface is not allowed. Interfaces describe behavior, not state.
9. Best Practices
-
Code to an Interface: When passing parameters to a function, ask for the Interface, not the concrete class.
fun testDrive(vehicle: Drivable)is better thanfun testDrive(vehicle: Car), because now the function accepts Cars, Trucks, and Motorcycles!
10. Exercises
-
1.
Create an interface
Playablewith a methodplay().
-
2.
Create classes
GuitarandPianothat implementPlayable.
-
3.
Create a function
performConcert(instrument: Playable)that callsplay().
-
4.
Call
performConcert()using both instruments inmain().
11. MCQs with Answers
Q1. Can you instantiate an Interface directly (e.g., val x = MyInterface())?
a) Yes b) No
Answer: b) No.
How many interfaces can a single Kotlin class implement?
If an interface defines a method without a body, what must the implementing class do?
Q5. Can an Abstract Class hold actual state (state variables)? a) Yes b) No Answer: a) Yes.
Q6. Can a class inherit from multiple Abstract Classes? a) Yes b) No, abstract classes are still classes and follow Single Inheritance rules Answer: b) No.
When a method in an abstract class is marked abstract, what does it mean?
Q9. Which concept is best used to model a "Can-Do" behavior (e.g., A Bird CAN FLY)? a) Abstract Class b) Interface Answer: b) Interface.
What is the benefit of making a function parameter an Interface type (e.g., fun handle(item: Clickable))?
12. Interview Questions
- Q: Explain the difference between an Interface and an Abstract Class. When would you use one over the other?
- Q: How do Interfaces solve the problem of Multiple Inheritance in Kotlin?