Advanced Programming
CO2039
Chapter 4: Abstract class & Interface
ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA
TP.HCM, 03/01/2025
CONTENT
INTRODUCTION TO ABSTRACT CLASS &
01
INTERFACE
02 INTERFACE IN DEPTH
03 CONCLUSION
04 ADVANCED SECTION
2
INTRODUCTION TO
01 ABSTRACT CLASS &
INTERFACE
3
What is Abstract class?
An abstract class is a class that cannot be instantiated. It serves as a blueprint
for other classes.
Key characteristics:
● Can include abstract methods (methods without implementation).
● May also include concrete methods (methods with implementation).
● Used when classes share common behavior but also require customization
in subclasses.
● Supports inheritance.
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?
An interface is a classlike construct that contains only constants and abstract
methods.
Key characteristics:
● Cannot be instantiated (only classes that implements interfaces can be
instantiated).
● Why not just use Abstract class? ⇒ Java does not permit multiple
inheritance but allow a class can implement multiple interfaces.
Can an interface be implemented by multiple classes?
● No instance variables; only constants allowed.
6
Creating an 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
Feature Abstract class Interface
Instantiation Cannot be instantiated Cannot be instantiated
Methods Can have abstract and Only abstract methods
concrete
Variables Can have instance Only constants allowed
variables
Inheritance Supports single Supports multiple
inheritance inheritance
Accessibility Modifiers Methods can have any Methods are implicitly
modifier public
16
ADVANCED
04 SECTION
17
Class diagram: Interface
18
Classes & Interfaces
Can you write a Java pseudocode for the above diagram?
19
The Cloneable interface
public class Circle extends Shape implements Cloneable {
private int x, y, radius;
// 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
ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA