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

4 - Java OOP (II)

Uploaded by

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

4 - Java OOP (II)

Uploaded by

Nguyen Huy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Java Basic for Tester

Java OOP (II)

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


Agenda

 Java Inheritance
 Java Method Overriding
 Java super Keyword
 Abstract Class & Method
 Java Interfaces
 Java Polymorphism
 Java Encapsulation

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


Java Inheritance

Inheritance is one of the key features of OOP (Object-oriented


Programming) that allows us to define a new class from an existing class.

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

Inheritance is one of the key features of OOP (Object-oriented


Programming) that allows us to define a new class from an existing class.

class Animal In Java, we use the extends keyword to


{ inherit from a class. Here, we have
// eat() method inherited the Dog class from the Animal
// sleep() method class.
}
class Dog extends Animal
{
// bark() method
}
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 4
Java Inheritance

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


Java Inheritance

Java Method overriding

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


Java Inheritance

Types of
inheritance

Single Multilevel Hierarchical Multiple Hybrid


inheritance inheritance inheritance inheritance inheritance

Single inheritance - Class B extends from class A only.


Multilevel inheritance - Class B extends from class A; then class C extends from class B.
Hierarchical inheritance - Class A acts as the superclass for classes B, C, and D.
Multiple inheritance - Class C extends from interfaces A and B.
Hybrid inheritance - Mix of two or more types of inheritance.

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


Java Inheritance

The most
important
use is the
reusability
of code.

Use
Inheritance
To achieve
runtime
polymorphis
m through
method
overriding

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


Java Method Overriding

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


Java Method Overriding

• Both the superclass and the subclass must have the


Rule 1 same method name, the same return type and the
same parameter list.

• We cannot override the method declared as final


Rule 2
and static.

• We should always override abstract methods of the


Rule 3
superclass.

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


Java super

The super keyword in Java is used in subclasses to access superclass


members (attributes, constructors and methods).

• To call methods of the superclass that is overridden in


Uses of super keyword

1
the subclass.

• To access attributes (fields) of the superclass if both


2 superclass and subclass have attributes with the same
name.

• To explicitly call superclass no-arg (default) or


3 parameterized constructor from the subclass
constructor.
1/5/2022 09e-BM/DT/FSOFT - ©FPT SOFTWARE – Fresher Academy - Internal Use 11
Java super

Access Overridden Methods of the superclass


If methods with the
same name are defined
in both superclass and
subclass, the method in
the subclass overrides
the method in the
superclass. This is
called method
overriding.

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


Java super

super to Call Superclass Method

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


Java Abstract Class and Abstract Methods

Java Abstract Class


An abstract class is a class that cannot be instantiated (we cannot create
objects of an abstract class). In Java, we use the abstract keyword to
declare an abstract class.

abstract class Animal {


//attributes and methods
}

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


Java Abstract Class and Abstract Methods

Java Abstract Method


We use the same keyword abstract to create abstract methods. An abstract
method is declared without an implementation.

abstract void makeSound();

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


Java Abstract Class and Abstract Methods

An abstract class can contain both abstract and non-abstract methods.

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


Java Abstract Class and Abstract Methods

Inheritance of Abstract Class


An abstract class cannot be instantiated. To access the members of an
abstract class, we must inherit it.

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


Java Abstract Class and Abstract Methods

Overriding of Abstract Methods


In Java, it is mandatory to
override abstract methods of the
superclass in the subclass. It is
because the subclass inherits
abstract methods of the
superclass.
Since our subclass includes
abstract methods, we need to
override them.

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


Java Abstract Class and Abstract Methods

Why Java Abstraction?

Abstraction is an important concept of object-oriented programming.


Abstraction only shows the needed information and all the unnecessary
details are kept hidden. This allows us to manage complexity by omitting or
hiding details with a simpler, higher-level idea.

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


Java Abstract Class and Abstract Methods

We use the keyword to create abstract classes and methods.

An abstract method doesn't have any implementation (method body).

A class containing abstract methods should also be abstract.

Key Points We cannot create objects of an abstract class.

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.

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


Java Interface

In Java, an interface defines a set of specifications that other classes must


implement.

An interface can include abstract methods and constants.

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


Java Interface
Like abstract classes, we cannot create objects of interfaces. However, we
can implement interfaces in other classes. In Java, we use the implements
keyword to implement interfaces.

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


Java Interface

Why use Interfaces?

1. Interfaces provide specifications that a class (which implements it)


must follow.
2. Similar to abstract classes, interfaces help us to achieve abstraction in
Java.
3. Interfaces are also used to achieve multiple inheritance in Java. If a
subclass is inherited from two or more classes, it's multiple inheritance.

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


Java Polymorphism

Polymorphism is an important concept of object-oriented programming. It


simply means more than one form. That is, the same entity (method or
operator or object) behaves differently in different scenarios.

Types of
Polymorphism

Run-time Compile-time
Overriding Polymorphism Polymorphism Overloading

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


Java Polymorphism

Run-time Polymorphism
In Java, run-time polymorphism can be achieved through method overriding.

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


Java Polymorphism

Compile-time Polymorphism
The compile-time polymorphism can be achieved through method overloading and
operator overloading in Java.

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


Java Polymorphism

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


Java Encapsulation

Encapsulation is one of the key features of object-oriented programing.


Encapsulation refers to the bundling of fields and methods inside a single
class.

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


Java Encapsulation

Java Data Hiding

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

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


Java Encapsulation

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


Java Encapsulation

Why Encapsulation?

1. In Java, encapsulation helps us to keep related fields and methods


together, which makes our code cleaner and easy to read.

2. It helps to control the modification of our data fields.

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


Thank you

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

You might also like