Codele / Advanced programming
Abstract classes and interfaces
Sometimes a part of a program only cares what an object can do, not what it is. A promise such as “anything payable must have pay()” lets many different objects fit in the same slot.
- An interface is a promise about what an object can do, written as names only, with no code inside. “Anything Payable must have pay().”
- An abstract class is a parent class with some methods left blank. Children must fill them in.
- Code that asks only for the promise (the checkout) works with every object that keeps it, even ones written later.
- Java, C#, C++ and Go refuse a class that forgets a promised method before the program runs. Python complains while running, when the object is made, and JavaScript only when the method is called.
- JavaScript has no interface keyword, so a parent class that throws an error stands in. Go has no inheritance: any type with a Pay method counts as Payable.
Loading the playable lesson…