Bahir Dar University ባሕር ዳር ዩኒቨርሲቲ
Bahir Dar Institute of Technology ባሕር ዳር ቴክኖሎጂ ኢንስቲትዩት
Faculty of Computing ኮምፒዩትንግ ፋኩሊት
Object Oriented Programming
Course Code: SEng2071
Target Group: 2nd Year Software Engineering
Instructor: Haileyesus D.
Object Oriented Programming
CHAPTER THREE ENCAPSULATION
&
INHERITANCE
Chapter Outline
01 Encapsulation
What is Encapsulation, How to Achieve encapsulation in Java,
Learn encapsulation with examples, Benefits of encapsulation
02 Inheritance
What is inheritance, Super-class, Sub-class, extends keyword,
super keyword, types of inheritance, advantages of inheritance
Encapsulation
Encapsulation
Encapsulation: in the process of wrapping the data (variables) and
code acting on the data (methods) together as a single unit.
In encapsulation, the variables of a class will be hidden from other
classes, and can be accessed only through the methods of their
current class. Therefore, it is also known as data hiding.
Compiled by : Haileyesus Demissie Object Oriented Programming 5
Encapsulation
Other way to think about encapsulation is, it is a protective shield
that prevents the data from being accessed by the code outside this
shield.
To achieve encapsulation in Java:
Declare the variables of a class as private.
Provide public setter and getter methods to modify and view the
variables values.
Compiled by : Haileyesus Demissie Object Oriented Programming 6
Encapsulation
To understand what is
encapsulation in detail
consider the following
bank account class
with deposit and show
balance methods
Compiled by : Haileyesus Demissie Object Oriented Programming 7
Encapsulation
Suppose a hacker managed to gain access to the code of your bank
account. Now, he tries to deposit amount -100 into your account by two
ways.
Approach 1: He tries to deposit an invalid amount (say -100) into your bank
account by manipulating the code.
No, because the variable in the
class account_balance is
declared as private. So It can
only be accessed with the
methods defined in the class. No
other class or object can access
it.
Now, the question is – Is that possible?
Compiled by : Haileyesus Demissie Object Oriented Programming 8
Encapsulation
Approach 2: Hacker's first approach failed to deposit the amount.
Next, he tries to do deposit an amount -100 by using "deposit"
method.
But method implementation has a check for negative values.
Compiled by : Haileyesus Demissie Object Oriented Programming 9
Encapsulation
Compiled by : Haileyesus Demissie Object Oriented Programming 10
Encapsulation
Benefits of Encapsulation
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.
The users of a class do not know how the class stores its data. A class
can change the data type of a field and users of the class do not need
to change any of their code.
Compiled by : Haileyesus Demissie Object Oriented Programming 11
Inheritance
12
Inheritance
Inheritance: can be defined as the process where one class acquires
the properties (methods and fields) of another.
Super Class: The class whose features are inherited is known as super
class(or a base class or a parent class).
Sub Class: The class that inherits the other class is known as sub
class(or a derived class, extended class, or child class).
The subclass can add its own fields and methods in addition to the
superclass fields and methods.
Compiled by : Haileyesus Demissie Object Oriented Programming 13
Inheritance
extends is the keyword used to inherit the properties of a class.
Syntax:
Class super_class_name {
……….
……….
}
Class sub_class_name extends super_class_name{
………..
…………
}
Compiled by : Haileyesus Demissie Object Oriented Programming 14
Inheritance
Following is an example demonstrating Java inheritance. In this
example, you can observe two classes namely Calculation and
My_Calculation.
Using extends keyword, the My_Calculation inherits the methods
addition() and Subtraction() of Calculation class.
Compiled by : Haileyesus Demissie Object Oriented Programming 15
Inheritance
Compiled by : Haileyesus Demissie Object Oriented Programming 16
Inheritance
Output
Compiled by : Haileyesus Demissie Object Oriented Programming 17
Inheritance
In the given program, when an object to My_Calculation class is
created, a copy of the contents of the superclass is made within it.
That is why, using the object of the subclass you can access the
members of a supeclass.
Compiled by : Haileyesus Demissie Object Oriented Programming 18
Inheritance
The Superclass reference variable can hold the subclass object, but
using that variable you can access only the members of the
superclass, so to access the members of both classes it is
recommended to always create reference variable to the subclass.
You can instantiate the class as given below. But using the superclass
reference variable (cal in this case) you cannot call the method
multiplication(), which belongs to the subclass My_Calculation.
Compiled by : Haileyesus Demissie Object Oriented Programming 19
Inheritance
A subclass inherits all the members (fields, methods, and nested
classes) from its superclass. Constructors are not members, so they
are not inherited by subclasses, but the constructor of the superclass
can be invoked from the subclass.
Compiled by : Haileyesus Demissie Object Oriented Programming 20
Inheritance
The super keyword is similar to this keyword.
Usage of super keyword
It is used to differentiate the members of superclass from the
members of subclass, if they have the same names.
It is used to invoke the superclass constructor from subclass
Compiled by : Haileyesus Demissie Object Oriented Programming 21
Inheritance
If a class is inheriting the properties of another class. And if the
members of the superclass have the same names as the sub class, to
differentiate these variables we use super keyword as shown below.
super.variable_Name;
super.method_Name();
Compiled by : Haileyesus Demissie Object Oriented Programming 22
Inheritance
Compiled by : Haileyesus Demissie Object Oriented Programming 23
Inheritance
Output
Display Method of superclass
Compiled by : Haileyesus Demissie Object Oriented Programming 24
Inheritance
If a class is inheriting the properties of another class, the subclass
automatically acquires the default constructor of the superclass. But
if you want to call a parameterized constructor of the superclass, you
need to use the super keyword.
super(values);
A super() call in the constructor of a subclass will result in the
execution of the relevant constructor from the superclass, based on
the signature of the call.
Compiled by : Haileyesus Demissie Object Oriented Programming 25
Inheritance
Example: Invoking Super class constructor
Output
Compiled by : Haileyesus Demissie Object Oriented Programming 26
Inheritance
There are different kinds of inheritance:
Single inheritance
Multi level inheritance
Hierarchical inheritance
Multiple inheritance
Compiled by : Haileyesus Demissie Object Oriented Programming 27
Inheritance
Compiled by : Haileyesus Demissie Object Oriented Programming 28
Inheritance
Compiled by : Haileyesus Demissie Object Oriented Programming 29
Inheritance
Advantage of inheritance is code reusability.
Because when a class inherits another class, it has all properties of
the base class and it adds some new properties of its own.
Compiled by : Haileyesus Demissie Object Oriented Programming 30
THANK YOU
“Any fool can write code that a
computer can understand.
Good programmers write code
that humans can understand. ”
Compiled by : Haileyesus Demissie Object Oriented Programming 31