4 - Java OOP (II)
4 - Java OOP (II)
Java Inheritance
Java Method Overriding
Java super Keyword
Abstract Class & Method
Java Interfaces
Java Polymorphism
Java Encapsulation
class Animal
{
// eat() method
// sleep() method
}
class Dog extends Animal
{
// bark() method
}
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 3
Java Inheritance
Types of
inheritance
The most
important
use is the
reusability
of code.
Use
Inheritance
To achieve
runtime
polymorphis
m through
method
overriding
1
the subclass.
To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass.
A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract,
it's not mandatory to override abstract methods.
We can access the static attributes and methods of an abstract class using the reference of the abstract class.
Types of
Polymorphism
Run-time Compile-time
Overriding Polymorphism Polymorphism Overloading
Run-time Polymorphism
In Java, run-time polymorphism can be achieved through method overriding.
Compile-time Polymorphism
The compile-time polymorphism can be achieved through method overloading and
operator overloading in Java.
protected - visible
public - visible from private - visible from default - visible within
within the package, and
anywhere. only within the class the package
among its subclasses
Why Encapsulation?