CMSC 203 Module 6
CMSC 203 Module 6
No State:
• Interfaces typically don't have fields or state.
• They are purely a contract for methods.
Method Signatures:
• They define only method signatures, not
implementations.
Implemented by Classes:
• Classes implement interfaces and provide concrete
definitions for the methods.
Key Points About Implementing
Interfaces
All methods must be implemented:
• If a class implements an interface, it must provide
concrete implementations for all the methods in the
interface, or it must be declared abstract.
Multiple inheritance:
• In languages that support multiple interface
inheritance (e.g., Java, C#), a class can implement
multiple interfaces, gaining behaviors from all of
them.
Interfaces promote flexibility:
• By coding to interfaces, you can create flexible,
reusable code that is easier to maintain and extend.
Difference between Interfaces and
Abstract Classes
Methods Implementation
• Interface: All methods are abstract by default (in many
languages like Java). They only contain method
signatures, with no method bodies.
Multiple Inheritances
• Interface: A class can implement multiple interfaces,
allowing for multiple inheritance of behavior. This
provides flexibility by enabling a class to inherit
behavior from multiple sources.
• Abstract Class: A class can inherit from only one
abstract class (single inheritance) in many languages
like Java. However, abstract classes can implement
multiple interfaces, combining both approaches.
Fields (State)
• Interface: Typically, interfaces cannot have fields
(member variables) to hold state. They only define
method signatures.
• Abstract Class: Can have fields, allowing it to
maintain a state. These fields can be inherited by the
subclasses and shared among them, or overridden with
specific values.
Constructor
• Interface: Interfaces cannot have constructors because
they don’t maintain any state or perform initialization.
They are purely abstract.
• Abstract Class: Can have constructors, which can be
called by subclasses to initialize common fields or
perform setup operations.
Usage Scenario
• Interface: Use interfaces when you want to define a
common contract that multiple classes can implement,
even if those classes don’t share a common ancestor or
any default behavior. It is used to define capabilities.
• Abstract Class: Use abstract classes when you want to
provide a base class with shared functionality that all
subclasses will inherit, but some methods still need to
be implemented by the subclass. It is used to define a
template.
Access Modifiers
• Interface: In many languages, all methods in an
interface are implicitly public and must be
implemented as public in the implementing classes.
Interfaces typically do not allow methods with other
access levels (private, protected).
• Abstract Class: Abstract classes allow all types of
access modifiers (public, protected, private) for their
methods and fields. This makes abstract classes more
flexible in terms of encapsulation.
Key Points about Interface inheritance
Extending an Interface:
• In most languages, interfaces can inherit other
interfaces using the extends keyword (e.g., Java,
TypeScript) or similar syntax.
Multiple Inheritance:
• Interfaces can extend multiple interfaces, allowing
them to combine contracts from different sources.
Implementation:
• A class that implements a child interface must provide
implementations for all methods declared in the entire
interface hierarchy (both parent and child interfaces).
THANKS!
CREDITS: This presentation template was created by Slidesgo, and
includes icons by Flaticon, and infographics & images by Freepik