0% found this document useful (0 votes)
40 views

Unit05 - Advanced OOP in Java

Uploaded by

lamg njfi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Unit05 - Advanced OOP in Java

Uploaded by

lamg njfi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

ADVANCED OOP WITH JAVA

Instructor:

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 1


Table of contents
◊ Polymorphism
P Types of Polymorphism
P Method Overloadding
P Method Overriding
P Static and Dynamic binding

◊ Abstraction
P Abstract class
P Interfaces

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 2


Learning Approach

Noting down the key


concepts in the class

Completion of the project


on time inclusive of Analyze all the examples /
individual and group code snippets provided
activities

!"#$%&'()*+&&,*",-).$#)
/)0,"",#)',/#%1%& /%-)
+%-,#*"/%-1%& $.)"21*)
Study and understand Study and understand
all the artifacts 3$+#*,4 the self study topics

Completion of the self


Completion and submission
review questions in the lab
of all the assignments, on time
guide

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 3


Section 1

POLYMORPHISM

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 4


4 major principles of OOP
v Inheritance
v Encapsulation
v Polymorphism
v Abstraction

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 5


Polymorphism Overview (1/2)

§ In object-oriented programming, polymorphism (from the


Greek (Hy Lạp) meaning "having multiple forms").
P Polymorphism is derived from 2 greek words: poly and morphs.
P The word "poly" means many and "morphs" means forms.

§ There are two types of polymorphism in java:


ü compile time polymorphism: method overloading.
ü runtime polymorphism: method overriding.

one name, many forms.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 6


Polymorphism Overview (2/2)

Polymorphism is a generic term for


having many forms.

You can use the same name for several different things
P the compiler automatically figures out which version you wanted.
There are several forms of polymorphism supported in Java,
shadowing, overriding, and overloading.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use


7
Polymorphism example

<<Abstract>>
Shap
- String Color
Shap(Color)
+ String getColor()
+ void setColor()
+ abstract String draw()

Circle Rectangle

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 8


Polymorphism example (1/4)
public abstract class Shape {
private String color;

public Shape(String color) {


this.setColor(color);
}

public String getColor() {


return Color;
}

public void setColor(String color) {


Color = color;
}
// abstract method
abstract public String draw();
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 9


Polymorphism example (2/4)
public class Circle extends Shape {
/**
* @param color
*/
public Circle(String color) {
super(color);
}

@Override
public String draw() {
return "I'm a " + this.getColor() + " circle.";
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 10


Polymorphism example (3/4)
public class Rectangle extends Shape {
/**
* @param color
*/
public Rectangle(String color) {
super(color);
}

@Override
public String draw() {
return "I'm a " + this.getColor() + " rectangle.";
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 11


Polymorphism example (4/4)
public class PolymorphismExample {
private List<Shape> shapes = new ArrayList<Shape>();
public PolymorphismExample() {
Shape myFirstCircle = new Circle("Red");
Circle mySecondCircle = new Circle("Blue");
Rectangle myFirstRectangle = new Rectangle("Green");
shapes.add(myFirstCircle);
shapes.add(mySecondCircle);
shapes.add(myFirstRectangle);
}
public List<Shape> getShapes() {
return shapes;
}
public static void main(String[] args) {
PolymorphismExample example = new PolymorphismExample();
for (Shape shape : example.getShapes()) {
System.out.println(shape.draw());
}
} Output:
} I'm a Red circle.
I'm a Blue circle.
I'm a Green rectangle.
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 12
Types of Polymorphism

v There are two types of polymorphism in java:


P Static Polymorphism also known as compile time polymorphism

P Dynamic Polymorphism also known as runtime polymorphism

v Compile time Polymorphism (or Static polymorphism)


P Polymorphism that is resolved during compiler time is known as
static polymorphism. Method overloading is an example of compile
time polymorphism.

P Method Overloading: This allows us to have more than one


method having the same name, if the parameters of methods are
different in number, sequence and data types of parameters.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 13


Method Overloading
v Overloaded methods let you reuse the same method name in a class,
but with different arguments (and optionally, a different return type).
v There are two ways to overload the method in java:
P By changing number of arguments
P By changing the data type

v In a subclass,
P you can overload the methods inherited from the superclass.
P such overloaded methods neither hide nor override the superclass
methods
P they are new methods, unique to the subclass.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 14


Method Overloading
v Some main rules for overloading a method:
P Overloaded methods must change the argument list.
P Overloaded methods can change the return type.
P Overloaded methods can change the access modifier.
• With override: Cannot reduce the visibility

P A method can be overloaded in the same class or in a subclass.

Note: When I say method signature I am not talking about return type of the method, for
example if two methods have same name, same parameters and have different return type,
then this is not a valid method overloading example. This will throw compilation error.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 15


Types of Polymorphism
public class SimpleCalculator {
int add(int number1, int number2) {
return number1 + number2;
}
int add(int number1, int number2, int number3) {
return number1 + number2 + number3;
}
double add(double number1, double number2) {
return number1 + number2;
}
}

public class TestSimpleCalculator {

public static void main(String[] args) {


SimpleCalculator simpleCalculator = new SimpleCalculator();

System.out.println(simpleCalculator.add(10, 20));
System.out.println(simpleCalculator.add(10, 20, 30));
System.out.println(simpleCalculator.add(12.5, 20.5));
}
}
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 16
Method Overloading and Type Promotion

v One type is promoted to another implicitly if no matching datatype is


found:

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 17


Practice time
A program calculates and displays bonus amounts to pay various
types of employees. There are 3 separate departments, numbered 1,
2, and 3.
P Department 1 employees are paid a bonus based on their sales: If their sales
amount is over $5000 they get 5% of those sales, otherwise they get nothing.
P Department 2 employees are paid a bonus based on the number of units they sell:
They get $100 per unit sold, and an extra $50 per unit if they sell more than 25 units;
if they sell no units, they get nothing.
P Department 3 employees assemble parts in the plant and are paid a bonus of 10
cents per part if they reach a certain level: Part-time employees must assemble more
than 250 parts to get the 10-cent-per-part bonus, and full-time employees must
assemble more than 700.

Write a set of 3 overloaded methods called getBonus() that works with


the program below, according to the specifications described above.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 18


Method Overriding
v Declaring a method in subclass which is already present in parent
class is known as method overriding.

v The main advantage of method overriding is:


P the class can give its own specific implementation to a inherited method
without even modifying the parent class(base class).

v Rules of method overriding in Java:


P Method name: method must have the same name as in the parent class.

P Argument list: must be same as that of the method in parent class,

P Access Modifier: cannot be more restrictive than the overridden method of


parent class.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 19


Overriding Examples
class Mammal {
String makeNoise() {
return "generic noise";
}
}

class Zebra extends Mammal {


String makeNoise() {
return "bray";
}
}

public class ZooKeeper {


public static void main(String[] args) {
Mammal m = new Zebra();
System.out.println(m.makeNoise());
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 20


A real example of Method Overriding
v Consider a scenario where Bank is a class that provides functionality to get
the rate of interest. However, the rate of interest varies according to banks.
v For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate
of interest.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 21


Shadowing
v This is called shadowing—name in class Dog shadows name in class
Animal
public class Animal {
String name = "Animal";
public void speak() {
System.out.println("generic speak!");
}
public static void main(String args[]) {
Animal animal1 = new Animal();
Animal animal2 = new Dog();
Dog dog = new Dog();
System.out.println(animal1.name + " " + animal2.name +" " + dog.name);
}
}
class Dog extends Animal {
String name = "Dog";
public void speak() {
System.out.println("dog speak!");
}
}

Animal Animal Dog

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 22


Static and Dynamic binding
v Static binding:
P The binding which can be resolved at compile time by compiler is known as
static or early binding.
P The binding of static, private and final methods is compile-time. The reason
is that the these method cannot be overridden and the type of the class is
determined at the compile time.
v Dynamic binding:
P When compiler is not able to resolve the call/binding at compile time, such
binding is known as Dynamic or late Binding.
P Method Overriding is a perfect example of dynamic binding as in
overriding both parent and child classes have same method and in this case
the type of the object determines which method is to be executed.
P The type of object is determined at the run time so this is known as dynamic
binding.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 23


Static and Dynamic binding

Static binding Dynamic binding


Happens at Compile time Happens at Runtime
Actual object is not used Actual object is used
Also known as Early binding Also known as Late binding
Speed is high Speed is low
Ex: - Method Overloading Ex: - Method Overriding

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 24


Static binding (Hiding method)
v If a subclass defines a class method with the same
signature as a class method in the superclass, the
method in the subclass hides the one in the superclass.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 25


Static binding (Hiding method)
public class TestOverridingAndHiding {
public static void main(String[] args) {
Circle myCircle = new Circle();
Shape myShape = myCircle;

Shape.testClassMethod();
myShape.testInstanceMethod();
}
}
Output:
The class method in Shape.
The instance method in Circle.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 26


Static binding
public class Boy extends Human {
public static void walk() {
System.out.println("Boy walks");
}

public static void main(String args[]) {

/* Reference is of Human type and object is


* Boy type
*/
Human obj = new Boy();
/* Reference is of HUman type and object is
* of Human type.
*/
Human obj2 = new Human();
obj.walk();
obj2.walk();
}
}

class Human {
public static void walk() {
System.out.println("Human walks"); Output:
} Human walks
} Human walks

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 27


Dynamic binding
public class Boy extends Human {
public void walk() {
System.out.println("Boy walks");
}

public static void main(String args[]) {

/* Reference is of Human type and object is


* Boy type
*/
Human obj = new Boy();
/* Reference is of HUman type and object is
* of Human type.
*/
Human obj2 = new Human();
obj.walk();
obj2.walk();
}
}

class Human {
public void walk() {
System.out.println("Human walks"); Output:
Boy walks
} Human walks
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 28


Covariant Return Type
v The covariant return type specifies that the return type may vary in the same
direction as the subclass.
v Before Java5, it was not possible to override any method by changing the
return type.
v But now, since Java5, it is possible to override method by changing the return
type if subclass overrides any method whose return type is Non-Primitive but it
changes its return type to subclass type.
v Example:
class A{
A get(){return this;}
}

class B extends A{
B get(a){return this;}
void message(){System.out.println("welcome to covariant return type");
}

public static void main(String args[]){


new B().get().message();
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 29


instanceof operator
v The java instanceof operator is used to test whether the object is an
instance of the specified type (class or subclass or interface).
v The instanceof is also known as type comparison operator because it
compares the instance with type.
P It returns either true or false.
P If we apply the instanceof operator with any variable that has null value, it
returns false.
v Example:
class Animal{} Output:
class Dog extends Animal{//Dog inherits Animal
public static void main(String args[]){ true
Animal a = new Animal(); true
Dog d = new Dog();
System.out.println(a instanceof Animal);//true
System.out.println(d instanceof Animal);//?
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 30


instanceof operator
Downcasting

v When Subclass type refers to the object of Parent class, it is known as


downcasting.
v Let's see the example, downcasting be performed without the use of
instanceof operator:
P The actual object that is referred by a, is an object of Dog class. So if we downcast
it, it is fine.

class Animal {
}

public class Dog extends Animal {

static void cast(Animal a) {


Dog d = (Dog) a;
System.out.println("Downcasting performed");
}

public static void main(String[] args) {


Animal animal = new Dog();
cast(animal);
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 31


instanceof operator
v But what will happen if we write:
Animal animal = new Animal();
Dog d = cast(animal);

v You faced with an exception:


Exception in thread "main" java.lang.ClassCastException: fa.training.jpe.Animal cannot be cast to
fa.training.jpe.Dog

v To resolve this problem, should perform downcasting with java instanceof


operator. Re-write code:
class Animal {}

public class Dog extends Animal {

static void cast(Animal a) {


if (a instanceof Dog) {
Dog d = (Dog) a;
System.out.println("Downcasting performed");
}
}

public static void main(String[] args) {


Animal animal = new Animal();
cast(animal);
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 32


Section 2

ABSTRACTION

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 33


4 major principles of OOP
v Inheritance
v Encapsulation
v Polymorphism
v Abstraction

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 34


Abstraction Overview
v Abstraction is a process of hiding the implementation details and
showing only functionality to the user.

§ Example:
ü Sending sms, you just type the text and send the message.
You don't know the internal processing about the message
delivery
It shows only important things to the user and
hides the internal details

Focus on what the object does instead of how


it does it.

§ There are two ways to achieve abstraction in


java:
ü Abstract class (0 to 100%)
ü Interface (100%)

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 35


Purposes of Abstraction
v Abstraction is used to manage complexity.
P For example: "sending SMS where you type the text and send the
message, you don't know the internal processing about the
message delivery".
v Abstraction lets you focus on what the object does instead
of how it does it.
v There are two ways to achieve abstraction in java
P Abstract class (0 to 100%)
P Interface (100%)

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 36


Purposes of Abstraction

v Abstraction is used to manage complexity.

§ All Square, Rectangle, Circle and


Triangle:
ü Have color
ü Can display
§ A shape may has all above characteristics:
ü So we call “Shape” is an abstract type
of Square, Rectangle, Circle and
Shape Triangle.
More abstract

Square Rectangle Circle Triangle More specific


09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 37
Go abstraction
v Then, we analysis deeper:
P A rectangle has four sides with lengths w and h
P A square has all of the characteristics of a rectangle; in addition, w = h
v So, square is a type of rectangle, or, rectangle can be an abstract type
of square
More abstract
Shape

Rectangle

Square Circle Triangle


More specific
09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 38
Abstract Class
v A class which is declared as abstract is known as an abstract class.
P It can have abstract and non-abstract methods.
P It needs to be extended and its method implemented.
P It cannot be instantiated.
v Rules for Java Abstract class:

• An abstract class must be declared with an abstract keyword.


1

• It can have abstract and non-abstract methods.


2

• It cannot be instantiated.
3

• It can have constructors and static methods also.


4
• It can have final methods which will force the subclass not to
5 change the body of the method.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 39


Abstract Class and Abstract method
v An abstract class can not be instantiated (you are not allowed to
create object of Abstract class), but they can be subclassed.
Shape shape = new Circle();
v When an abstract class is subclassed, the subclass usually provides
implementations for all of the abstract methods in its parent class.
P However, if it does not, the subclass must also be declared abstract.
v Abstract method:
P An abstract method is a method that is declared without an implementation.
Example: abstract void moveTo(int x, int y);
P If a class includes abstract methods, the class itself must be declared
abstract.
P All of the methods in an interface are implicitly abstract: so the abstract
modifier is not used with interface methods.

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 40


Interface
v An interface in Java is a blueprint of a class. It has static constants
and abstract methods.
v The interface in Java is a mechanism to achieve abstraction.
P There can be only abstract methods in the Java interface, not method body.
P It is used to achieve abstraction and multiple inheritance in Java.

v Why use Java interface?

• It is used to achieve abstraction.


1
• By interface, we can support the functionality of
2 multiple inheritance.

• It can be used to achieve loose coupling.


3

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 41


Interface
v Syntax:
[public] interface <InterfaceName>[extends SuperInterface] {
// Interface Body
}
v Example:

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 42


Interface
v The Java compiler adds public and abstract keywords before the
interface method.
v Moreover, it adds public, static and final keywords before data
members.
v Consider the following figure:

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 43


Interfaces

ü An interface is a definition of
method prototypes and possibly
some constants (static final fields).
ü An interface does not include the
implementation of any methods.

ü A class can implement an interface, this means that it provides


implementations for all the methods in the interface.
v Java classes can implement any number of interfaces (multiple interface
inheritance).

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 44


Java Interface Example: Bank
v Let's see another example of java interface which provides the implementation
of Bank interface.
interface Bank {
float rateOfInterest();
Output:
}
ROI: 9.15
class SBI implements Bank {
public float rateOfInterest() {
return 9.15f;
}
}

class PNB implements Bank {


public float rateOfInterest() {
return 9.7f;
}
}

public class TestInterface {

public static void main(String[] args) {

Bank b = new SBI();


System.out.println("ROI: " + b.rateOfInterest());
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 45


Classes and interfaces
v As shown in the figure given below, a class extends another class, an
interface extends another interface, but a class implements an
interface.

v Multiple inheritance in Java by interface

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 46


Interfaces can be extended
v Example:
public interface Forward {
one interface can extend another.
void drive();
}
public interface Stop {
void park();
}
public interface Speed {
void turbo();
}
public class GearBox {
public void move() {
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 47


Interfaces can be extended (cont.)

v Example:
class Automatic extends GearBox implements Forward, Stop,
Speed
{
public void drive() {
System.out.println("drive()");
}
public void park() {
System.out.println("park()");
}
public void turbo() {
System.out.println("turbo()");
}
public void move() {
System.out.println("move()");
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 48


Interfaces can be extended (cont.)
v Example:
public class Car {
public static void cruise(Forward x) {
x.drive();
}
public static void park(Stop x) {
x.park();
}
public static void race(Speed x) {
x.turbo();
}
public static void move(GearBox x) {
x.move();
}
public static void main(String[] args) {
Automatic auto = new Automatic();
cruise(auto); // Interface Forward
park(auto); // Interface Stop
race(auto); // Interface Speed
move(auto); // class GearBox
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 49


Multiple inheritance in Java by interface
v Multiple inheritance is not supported in the case of class because of ambiguity.
v However, it is supported in case of an interface because there is no ambiguity.
v It is because its implementation is provided by the implementation class.
v Example:
interface Printable {
void print();
}

interface Showable {
void print();
}

class A4 implements Printable, Showable {


public void print() {
System.out.println("Printing and showing document");
}
}

public class TestInterface2 {


public static void main(String[] args) {
A4 a4 = new A4();
a4.print();
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 50


Java 8 Static Method in Interface
v Since Java 8, we can have method body in interface. But we need to
make it default method.
v Example:
interface Drawable { Output:
void draw();

default void msg() {


drawing rectangle
System.out.println("default method");
} default method
}

class Rectangle implements Drawable {


public void draw() {
System.out.println("drawing rectangle");
}
}

public class TestInterfaceDefault {


public static void main(String[] args) {
Drawable d = new Rectangle();
d.draw();
d.msg();
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 51


Java 8 Static Method in Interface
v Since Java 8, we can have static method in interface.
v Example:
interface Drawable { Output:
void draw();

static int cube(int x) {


drawing rectangle
return x * x * x;
}
27
}

class Rectangle implements Drawable {


public void draw() {
System.out.println("drawing rectangle");
}
}

public class TestInterfaceStatic {


public static void main(String[] args) {
Drawable d = new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 52


Abstract class and Interface
v Abstract class and interface both are used to achieve abstraction
where we can declare the abstract methods. Abstract class and
interface both can't be instantiated.
v But there are many differences between abstract class and interface
that are given below.
No. Abstract class Interface

1 Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.

2 Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.

3 Abstract class can have final, non-final, static and non-static Interface has only static and final variables.
variables.

4 Abstract class can have static methods, main method and Interface can't have static methods, main method or constructor.
constructor.

5 Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.

6 The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

7 Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 53


Summary
Polymorphism, which means "many forms,"
P is the ability to treat an object of any subclass of a base class as if it were an object
of the base class.

Abstract class is a class that may contain abstract methods and


implemented methods.
P An abstract method is one without a body that is declared with the reserved word
abstract

An interface is a collection of constants and method declarations.


P When a class implements an interface, it must declare and provide a method body for
each method in the interface

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 54


Thank you

09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 55

You might also like