Vietnam National University of HCMC
International University
School of Computer Science and Engineering
Object Oriented Programming
Design with classes and Interface
Instructor: Le Thi Ngoc Hanh, Ph.D
ltnhanh@[Link]
Semester – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Benefits of Polymorphism
Flexibility: allow objects to behave as if they belong to different
classes, making the code more adaptable.
Reusability: facilitate code reuse by enabling a single interface or
superclass method to be implemented in multiple ways by various
subclasses.
Maintainability and Scalability: Systems that use polymorphism
require fewer code changes when new classes are added or
existing ones are modified.
Understandability:
By enabling modular development, polymorphism makes the code
easier to understand.
Slide 2 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Lecture Objectives
A deeper look into
Inheritance & Polymorphism
stories
Slide 3 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Abstract Classes and Methods
An abstract class is a class declared using the keyword
abstract.
It
is used when we want to define a general concept
but not a complete implementation.
Abstract classes are typically used as superclasses in an
inheritance hierarchy.
They cannot be instantiated because they are
incomplete.
They provide a common design and structure for
subclasses.
Slide 4 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Concrete Classes and Methods
Classes that can be used to instantiate objects are called
concrete classes.
Such classes provide implementations of every method they
declare (some of the implementations can be inherited).
Abstract superclasses are too general to create real objects -
they specify only what is common among subclasses.
Concrete classes provide the specifics that make it reasonable to
instantiate objects.
Not all hierarchies contain abstract classes.
Slide 5 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Abstract vs Concrete Example
Slide 6 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Abstract vs Concrete Example
Assuming that new class is derived.
Problem!!!
Slide 7 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Before
After
Slide 8 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Slide 9 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Abstract vs Concrete Example
Polymorphism at run-time:
Slide 10 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
overriding
vs
overloading
vs
polymorphism
Slide 11 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
More Example
Without polymorphism With polymorphism
Slide 12 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Supertype and subtype
A supertype (parent class) represents a general abstraction
that captures common attributes and behaviors shared
across multiple related entities.
A supertype defines shared structure (state) and shared
behavior (methods); it enables code reuse and conceptual
modeling of “is-a” relationships.
A supertype is often:
Concrete class (can be instantiated), or
Abstract class (cannot be instantiated, used for modeling)
Interface modeling
Slide 13 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Supertype and subtype
A subtype (child class) is a class that Inherits from a
superclass (extends), OR Implements an interface
(implements)
Maintains is-a relationship
Example: FullTimeEmployee is-a Employee
PartTimeEmployee is-a Employee
Subtype provides HOW behavior is implemented
Slide 14 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Sample contexts
Slide 15 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Upcasting
Upcasting is the process of converting a subtype
object into a supertype reference.
Occurs automatically (implicit casting)
No explicit cast required
A reference
Always safe (no runtime risk)
cast
Enables polymorphism
B object
Slide 16 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Upcasting – Example
Slide 17 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Downcasting and instanceof
Downcasting is the process of converting a
supertype reference back into a subtype reference.
▪ Requires explicit cast
▪ May be unsafe (runtime error possible)
▪ Used when we need to access subtype-specific behavior
upcasting
downcasting
Invalid
downcasting
Slide 18 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Downcasting and instanceof
- Solution: Use instanceof to verify before casting:
- This solution can prevent runtime errors and ensures safe
downcasting.
- Good OOP design reduces need for downcasting:
Slide 19 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Abstract Class vs. Interface
An abstract class is used when we An interface defines a pure
want to: behavioral contract:
▪ Represent a base concept with shared state
▪ Specifies what must be done, not
and behavior
how
▪ Provide partial implementation
▪ Enforce some methods via abstract methods ▪ No object state (except constants)
Can have instance variables (state), Methods are implicitly public
constructors, concrete methods abstract; fields are public
(implemented), abstract methods static final
(unimplemented) Supports multiple
Supports single inheritance only implementation
Models “is-a” relationship with shared Models capability / role-based
structure design
Slide 20 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Abstract Class, Interface, or Regular Class?
Slide 21 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Boundary
InOOP, inheritance provides flexibility, but
unrestricted extension can:
Break intended behavior
Violate security or business rules
Introduce incorrect overriding
Java uses the final keyword to define boundaries
(restrictions)
Slide 22 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Boundary – final Class
A final class:
Cannot be extended
Prevents creation of subclasses
Syntax:
Slide 23 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Boundary – final Method
A final method:
Can be inherited
But cannot be overridden by subclasses
Slide 24 OOP – Spring 2026
VNU-HCM International University School of Computer Science and Engineering
Boundary - final Variable
A final variable:
Can be assigned only once
Value cannot be changed after initialization
A final variable must be initialized at declaration, or inside
constructor (for instance variables)
constant
Slide 25 OOP – Spring 2026