Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Encapsulation and Abstraction ppt
Nội dung xem thử
Mô tả chi tiết
Encapsulation and
Abstraction
Chapter 4
Objects contain data and methods to send and
receive messages. Data members of objects can have
a different scope or visibility. You use access
specifier to define the scope of data member and
method.
This chapter introduces the concept of abstraction
and encapsulation. It discusses the implementation
of encapsulation by using access specifiers. It also
explains the concept of using methods. In addition,
the chapter also discusses the static variables and
static functions.
In this chapter, you will learn to:
Define abstraction and encapsulation
Implement encapsulation by using access
specifiers
Use methods
Use static variables and static functions
Objectives
¤NIIT Encapsulation and Abstraction 4.3
Abstraction and encapsulation are important features of any OOPs language. Abstraction
involves extracting only the relevant information. Encapsulation involves packaging one
or more components together.
Consider the following example:
An automobile salesperson is aware that different people have different preferences.
Some people are interested in the speed of a car, some in its price, some in the engine, and
the some in its style. Although all of them want to buy a car, each of them is interested in
a specific attribute or feature. The salesman knows all the details of a car, but he presents
only the relevant information to a potential customer. As a result, the salesman practices
abstraction and presents only relevant details to customer.
In other words, abstraction means ‘looking for what you want’ in an object or a class.
Consider abstraction from the perspective of a programmer who wants a user to be able to
add items to a list. However, the detail of how a particular task in a program is
accomplished is hidden. Abstraction does not mean that information is unavailable. It
means that all the information exists, but only the relevant information is provided to the
user.
Encapsulation literally means ‘to enclose in or as if in a capsule’. Encapsulation is defined
as the process of enclosing one or more items within a physical or logical package. It
involves preventing access to nonessential details.
For example, when you plug in the cord of the vacuum cleaner and turn on the switch, the
vacuum cleaner starts. You do not see the complex processes needed to actually convert
electricity into suction power. In other words, the exact working of the cleaner has been
encapsulated. Therefore, encapsulation is also explained as information hiding or data
hiding because it involves hiding many of the important details of an object from the user.
Abstraction and encapsulation are different but related features. Abstraction enables you
to make the relevant information visible. Encapsulation enables you to package
information to implement the desired level of abstraction. Therefore, encapsulation assists
abstraction by providing a means of suppressing the nonessential details. Encapsulation
allows some information to be hidden but it also allows some information to be visible.
Introducing Abstraction and Encapsulation
Defining Abstraction
Defining Encapsulation