Chap4 Abstract Interface
Chap4 Abstract Interface
CO2039
03 CONCLUSION
04 ADVANCED SECTION
2
INTRODUCTION TO
01 ABSTRACT CLASS &
INTERFACE
3
What is Abstract class?
4
Example in Java
abstract class Shape {
abstract void draw(); // Abstract method
void info() { // Concrete method
System.out.println("This is a shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
5
What is Interface?
File InterfaceName.java:
modifier interface InterfaceName { ⇒ modifier is public or not used.
constants declarations;
methods signatures;
}
File ClassName.java:
modifier Class ClassName implements InterfaceName {
methods implementation;
}
7
Hands-on exercise
interface Drawable {
void draw(); // Abstract method
}
class Rectangle implements Drawable {
public void draw() {
System.out.println("Drawing Rectangle");
}
}
Add new interface Area to calculate the area of a shape and implement it in
Rectangle. Write main function to test.
8
INTERFACE IN
02 DEPTH
9
More in Interfaces (1)
Compile-Time Dependency Problem:
● Normally, methods from one class to another need compile-time presence for
signature checks.
● This creates rigid inheritance hierarchies, pushing shared functionality higher in the
class tree.
Interfaces to the Rescue:
● Allow dynamic method resolution at runtime.
● Decouple method definitions from the class inheritance hierarchy.
● Enable unrelated classes to implement the same interface.
Key Benefit: Promote composition over inheritance and avoid the rigidity of deep class
hierarchies. 10
More in Interfaces (2)
Dynamic Method Resolution:
● The actual method is determined at runtime, not compile time.
● Calling code interacts with an interface or abstract class reference,
not specific implementations.
● Benefit:
○ Decouples calling code from concrete classes.
○ Allows flexibility to add/change implementations without
modifying the calling code.
11
More in Interfaces - Example
interface Shape { void draw(); }
class Circle implements Shape {
public void draw() { System.out.println("Drawing a Circle"); }
}
class Square implements Shape {
public void draw() { System.out.println("Drawing a Square"); }
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle(); // Interface reference, dynamic resolution
shape.draw(); // Calls Circle's draw() method
shape = new Square(); // Switch to a different implementation
shape.draw(); // Calls Square's draw() method
}
} 12
Does interface solve diamond problem?
Interfaces help address the diamond problem in multiple inheritance, but indirectly:
● The diamond problem arises when a class inherits from two parent classes that
define methods with the same name and signature, leading to ambiguity.
● In Java:
○ Classes do not support multiple inheritance, which avoids this issue.
○ Interfaces support multiple inheritance, but there’s no ambiguity because a
class implementing multiple interfaces must explicitly define the methods it
inherits.
○ If a default method in two interfaces clashes, the implementing class must
override it to resolve the conflict explicitly.
13
Example: Interface in diamond problem
interface A {
default void show() { System.out.println("A"); }
}
interface B {
default void show() { System.out.println("B"); }
}
class C implements A, B {
// Resolving the diamond problem
@Override
public void show() {
A.super.show(); // Explicitly choose which interface method to
call
}
}
14
03 CONCLUSION
15
Abstract class vs. Interface
16
ADVANCED
04 SECTION
17
Class diagram: Interface
18
Classes & Interfaces
// Constructor
...
// Clone method
@Override
public Object clone() {
try { return super.clone(); } // Shallow copy
catch (CloneNotSupportedException ex) { return null; }
}
}
Main function:
Circle c1 = new Circle(1, 2, 3); // Create an instance of Circle
Circle c2 = (Circle) c1.clone(); // Clone c1 into c2
System.out.println("Cloned Circle: " + c2); // Verify cloning
20
Thank you for your
attention!
https://www.cse.hcmut.edu.vn