PSOOP-lab 08-interfaces in java-2024-25
PSOOP-lab 08-interfaces in java-2024-25
Nikahat Mulla
2
Outline
● Single vs Multiple Inheritance
● Java Interface
● Interface Syntax
● Implementing Interfaces
● Interface Example
● Extending Interfaces
● Polymorphism via Interfaces
● Example: Polymorphism via Interfaces
3
Interfaces
● An interface in Java is a collection of abstract methods and
constants. It defines a contract that classes must follow
without providing any implementation.
Interface Syntax
interface MyInterface {
int count; //constant
void myMethod(); // Abstract method
default void defaultMethod() {
System.out.println("Default method");
}
static void staticMethod() {
System.out.println("Static method");
}
}
6
Implementing Interfaces
• A class implements an interface using the implements keyword.
• The class must provide implementations for all the methods of the
interface.
class MyClass implements MyInterface {
@Override
public void myMethod() {
System.out.println("My Method");
}
}
Before Java 8, interfaces could only have abstract methods (methods without
implementation). This posed a major issue when you needed to update or
extend an existing interface without breaking the implementing classes.
1. Default methods must use the `default` keyword.
2. Default methods can be overridden in implementing classes.
3. If a class implements multiple interfaces with conflicting
default methods, it must override them.(use
InterfaceName.super.methodName() to resolve ambiguity.)
4. Default methods can call other interface methods.
9
class C implements A, B {
@Override
public void display() {
A.super.display(); // Call display from interface A
B.super.display(); // Call display from interface B
System.out.println("Custom logic in C");
}
}
10
Extending Interfaces
interface A {
void methodA();
}
interface B extends A {
void methodB();
}
class C implements B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}
14
Rules of Interfaces
interface InterfaceName {
// Constant variables (public, static, final by default)
int CONSTANT_VALUE = 100;
Question 1
Which of the following is true about interfaces?
a) An interface can have a constructor.
b) An interface can contain instance variables.
c) A class can implement multiple interfaces.
d) An interface can be instantiated.
Question 2
What happens if a class implements an interface
but does not define all its methods?
a) The class must be declared abstract.
b) The program will run but with warnings.
c) The missing methods will have default
implementations.
Question 3
Which of the following features was introduced in
interfaces in Java 8?
a) Constructors in interfaces
b) Private methods in interfaces
c) Default and static methods
d) Instance variables in interfaces
Answer : Class B
23
Problem Statement
A banking system has two interfaces SavingAccount and
CurrentAccount.
The SavingAccount account has method
getSimpleInterest() and CurrentAccont has method
getCompoudInterest(). Both these interfaces are
implemented by class Customer. Customer has data
members: account type, interest rate and balance. Add any
appropriate constructors and methods. Create a main class
which creates an array of customers. Ask the user to
choose the interest type and accordingly compute interest
and update the balance. Sort the array of customers
according to balance using Arrays.sort() by implementing
the Comparable interface.
25
Thank you