object oriented programming - abstraction & encapsulation

14
2SC2 – OOP C# Dudy Fathan Ali S.Kom Abstraction dan Encapsulation 2SC2 Dudy Fathan Ali, S.Kom (DFA) 2015 CEP - CCIT Fakultas Teknik Universitas Indonesia

Upload: dudy-ali

Post on 12-Apr-2017

60 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Object Oriented Programming - Abstraction & Encapsulation

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Abstraction dan Encapsulation2SC2

Dudy Fathan Ali, S.Kom (DFA)2015

CEP - CCITFakultas Teknik Universitas Indonesia

Page 2: Object Oriented Programming - Abstraction & Encapsulation

Abstraction

2SC2 – OOP C# Dudy Fathan Ali S.Kom

You are the salesman, please help the customer..

Hey, I need a Car.

Page 3: Object Oriented Programming - Abstraction & Encapsulation

Abstraction

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Hey, I need a Car.

You are the salesman, please help the customer..

Page 4: Object Oriented Programming - Abstraction & Encapsulation

Abstraction

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Abstraction involves extracting only the relevant information.

Courseware NIIT – Object Oriented Programming Using C# - 1

Page 5: Object Oriented Programming - Abstraction & Encapsulation

Abstraction

2SC2 – OOP C# Dudy Fathan Ali S.Kom

In a programmer’s perspective..

Display Car

Insert Car

Class Car

I want to Display the Car

Delete Car

Just because the user want to display it, doesn’t mean insert and delete function is unavailable.

Page 6: Object Oriented Programming - Abstraction & Encapsulation

Encapsulation

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.

www.techopedia.com

Private - Employee’s

Salary

I want to access ID Employee

Yes, because it’s Public

Public – Name, ID of Employee

Cashier

Human Resources Database

Page 7: Object Oriented Programming - Abstraction & Encapsulation

Human Resources Database

Encapsulation

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.

www.techopedia.com

Private - Employee’s

Salary

Public – Name, ID of Employee

I want to access my

friend’s salary

No, because it’s private!! It

doesn’t belongs to

cashier.

Cashier

Page 8: Object Oriented Programming - Abstraction & Encapsulation

Human Resources Database

Encapsulation

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.

www.techopedia.com

Private - Employee’s

Salary

Public – Name, ID of EmployeeHRD Manager

I want to access my

friend’s salary

Yes, because this data is belongs to

him

Yes, because this data is belongs to him and also it’s

public

Page 9: Object Oriented Programming - Abstraction & Encapsulation

Encapsulation

2SC2 – OOP C# Dudy Fathan Ali S.Kom

In a programmer’s perspective..

Private – Employee’s

Salary

Public – Name, ID of Employee

Access to all code in the program

Class HR_Data

Access to only members of the

same classYes, same

class!

method

method

Page 10: Object Oriented Programming - Abstraction & Encapsulation

Private

Encapsulation – Access Specifier

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.

www.techopedia.com

Public Protected

Internal

Protected Internal

Access Specifier

Page 11: Object Oriented Programming - Abstraction & Encapsulation

Encapsulation – Access Specifier

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.

www.techopedia.com

Page 12: Object Oriented Programming - Abstraction & Encapsulation

Encapsulation – Access Specifier

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.

www.techopedia.com

Class DisplayData{ public void Method1() { Console.WriteLine(“Public”); Method2(); }

private void Method2() { Console.WriteLine(“Private”); }}

Output :

Class Program :DisplayData dd = new DisplayData();

dd.Method1();

PublicPrivate

Access Specifier

Page 13: Object Oriented Programming - Abstraction & Encapsulation

Encapsulation – Access Specifier

2SC2 – OOP C# Dudy Fathan Ali S.Kom

Encapsulation, in the context of C#, refers to an object's ability to hide data and behavior that are not necessary to its user.

www.techopedia.com

Class DisplayData{ public void Method1() { Console.WriteLine(“Public”); Method2(); }

private void Method2() { Console.WriteLine(“Private”); }}

Output :

Error!! Because method2()

has a private access.

Class Program :DisplayData dd = new DisplayData();

dd.Method2();

Access Specifier

Page 14: Object Oriented Programming - Abstraction & Encapsulation

3SC4 PNJ – Q5 Dudy Fathan Ali S.Kom

Terima KasihDudy Fathan Ali S.Kom

[email protected]