Interfaces
Interfaces
Interfaces
SIT Internal
Learning outcomes
• Motivation for
Interfaces
• Introduction to
interfaces
• Key features
SIT Internal
Abstraction
• Hiding complex, low-level details with a simpler, high-level layer
- displayDetail()
Person
Teacher Student
Teacher Student
- department - department
- salary() - calculateCGPA()
Teaching Assistant
SIT Internal
- displayDetail()
Person
Teacher Student
Teacher Student
- department - department
- salary() - calculateCGPA()
Teaching Assistant
SIT Internal
- displayDetail()
Person
Teacher Student
Teacher Student
- department - department
- salary() - calculateCGPA()
Teaching Assistant
SIT Internal
- displayDetail()
Person
Teacher Student
Teacher Student
- department - department
- salary() - calculateCGPA()
TeachingAssistant
- department
Teaching Assistant
SIT Internal
8
SIT Internal
Interfaces
Interfaces
• Blueprint for a group of related methods that a class must
implement.
Teacher Student
// Teacher interface // Student interface
public interface public interface Student {
Teacher { void study();
void teach(); }
}
@Override
public void study() {
System.out.println(name + " (Teaching Assistant)
is also studying.");
}
SIT Internal
Interfaces: Implementation
• Cannot be instantiated
• A class that implements the interface should implement all the
methods defined in the interface (like a contract)
• Exception: Only if its an abstract class then it does not have to
implement all the methods defined by that interface
SIT Internal
An abstract class contain both abstract An interface contain only abstract method.
Methods and concrete(non abstract) method
Abstract class contain any type of variable Interface contain only static final variable
Variables (constant)
SIT Internal
Conclude
• A blueprint for a group of related methods that a class must
implement
• They allow unrelated classes to work together by providing a
common set of methods that they all agree to implement.
• Allows achieving abstraction and polymorphism