SlideShare a Scribd company logo
1
A Programme Under the compumitra Series
Programming Primer:
Encapsulation and Abstraction
LAB WORK GUIDE
2
OUTLINE
 Encapsulation and Abstraction Using
C# in asp.net
Creating Example Event Handler.
Example Output an Explanation.
Home Exercise.
Summary Review.
3
Encapsulation and Abstraction
Using C# in asp.net
4
EncapsulationCS – Creating Button and Label Template
Button with 'Text' Property set
to 'Encapsulation'.
Two Labels to hold the place
for output display with 'text'
property set to NULL (blank).
• Follow Standard Website Creation Steps from C# and set your path
to C:Learner<student-id>ProgrammingPrimerEncapsulationCS
• Now Create the Execution Event Handler as a button and output
place holders as follows.
5
EncapsulationCS –Copy and Paste Code
Go to 'Default.aspx.cs' and 'Paste' the Code in
'Button1_Click' handler.
Copy this Code
Employee emp = new Employee();
emp.EmployeeID = 100;
emp.Salary = 8500.30;
Label1.Text = "Employee ID = " + emp.EmployeeID.ToString();
Label2.Text = "Employee Salary = " + emp.Salary.ToString();
Employee emp = new Employee();
emp.EmployeeID = 100;
emp.Salary = 8500.30;
Label1.Text = "Employee ID = " + emp.EmployeeID.ToString() ;
Label2.Text = "Employee Salary = " + emp.Salary.ToString() ;
6
EncapsulationCS –Copy Code
class Employee
{
private int _employeeID;
private double _salary;
public int EmployeeID //Properties
{
set
{ _employeeID = value; }
get
{ return _employeeID; }
}
public double Salary //Properties
{
set
{ _salary = value; }
get
{ return _salary; }
}
}
Copy this Code
7
EncapsulationCS –Paste Code
Run Code By
pressing 'F5'
Paste code after the End
of '_Default' class
class Employee
{
private int _employeeID;
private double _salary;
public int EmployeeID //Properties
{
set
{ _employeeID = value; }
get
{ return _employeeID; }
}
public double Salary //Properties
{
set
{ _salary = value; }
get
{ return _salary; }
}
}
Label1.Text = emp.EmployeeID.ToString();
Label2.Text = emp.Salary.ToString();
8
Employee emp = new Employee();
emp.EmployeeID = 100;
emp.Salary = 8500.30;
Label1.Text = emp.EmployeeID.ToString();
Label2.Text = emp.Salary.ToString();
EncapsulationCS –Output
Output after executing the
handler using the button.
This 'output' is generated, because we
are trying to fill values in public
properties that are accessible outside
class even though the values are
actually filled in private values such
as ' _salary ' '_employeeID'.
We are able to change this as
this is defined as 'public' in
original class.
Don't worry we shall soon learn 'private' declaration too.
9
class Employee
{
private int _employeeID;
private double _salary;
public int EmployeeID //Properties
{
set
{ _employeeID = value; }
get
{ return _employeeID; }
}
public double Salary //Properties
{
set
{ _salary = value; }
get
{ return _salary; }
}
}
EncapsulationCS – Example Explanation
This has been defined as 'Private'. This
means that only code written inside
class Employee can modify it.
This is 'Public' as we would like to use a
way to change this property from an
object based on class Employee.
'Public' and 'Private' declarations are known
as access modifiers and they provide the
primary technique of ENCAPSULATION.
Are there other such modifiers? Surely Yes.
10
class Employee
{
private int _employeeID;
private double _salary;
public int EmployeeID //Properties
{
set
{ _employeeID = value; }
get
{ return _employeeID; }
}
public double Salary //Properties
{
set
{ _salary = value; }
get
{ return _salary; }
}
}
EncapsulationCS – Example Explanation for ABSTRACTION
Lets Focus on this class 'Employee'.
Suppose I have to define an additional
property 'employ_name' then it is most
likely that I shall define it within this
class.
Means to external world 'Employee' is
an abstract representation of all
behaviours(methods) and
states(properties) of employee and it is
supposed to encapsulate everything
related to this class.
So! Encapsulation helps to attain
ABSTRACTION. Abstraction also
means that class is just a template or a
map. Actual embodiment or physical
usage is where we create an object
based on a class.
11
emp._employeeID = 100;
emp._salary = 8500.30;
Label1.Text = "Employee ID = " + emp._employeeID.ToString();
Label2.Text = "Employee Salary = " + emp._employeeID.ToString();
emp._employeeID = 100;
emp._salary = 8500.30;
Label1.Text = "Employee ID = " + emp._employeeID.ToString();
Label2.Text = "Employee Salary = " +
emp._employeeID.ToString();
EncapsulationCS – Error Trial
Make necessary changes here and 'Run' code.
Here we shall try to use the same
code with a change to try using
'Private' variable _employeeID
and _salary.
12
EncapsulationCS–Output after changing code
This program will generate a 'Compiler
Error Message: CS0122:
'Employee._employeeID' is not accessible as
it is 'Private'.
Here we see that variable '_employeeID' and '_salary' is actually hidden
behind the class ' Employee'. We can not initialize out side the class by using
object 'emp' of class 'Employee'.
This Error Output Actually proves that the concept of
'ENCAPSULATION' works. Code in Error but we are still very happy.
13
EncapsulationCS : Home Exercise
 You are required to make a program where you can declare a
class 'Mobile'.
 Make a public method sendsms( ).
 Make a protected method typetext( ).
 Call typetext( ) within sendsms( ).
 Now write some eventhandler to show functionality if protected could
be called outside class or not.
 Now further create a class derived from class 'mobile' and name it as
'iphone'.
 Try to use the typetext( ) method now.
We have told you about 'Private' and 'Public' access modifiers. Another common
access modifier used is 'Protected'
A 'Protected' access modifier allows access within a derived (child) class, but not
outside class.
Are there other access modifiers. Try to search internet to find-out and understand.
Difficult?. 'Facing Difficulties is the prime goal of a programmer'. Play some music
and relax. Next is just a summary slide.
14
EncapsulationCS : Learning Summary Review
 Concept of Encapsulation
 Encapsulation helps to provide controlled access to outside classes.
 Encapsulation also helps to bundle similar things together.
 Concept of Abstraction
 Encapsulation helps to attain Abstraction.
 Abstraction also relates to non-physical nature of classes.
 Trying to use a 'Private' data outside the class will give an
error. For this purpose 'Public' declaration should be done.
 A 'Protected' modifier is suitable for neat definition of access
restriction for access to derived classes only.
 Private properties or methods also serve an additional
purpose. If a property or method is private in a class means
the property or methods of same name can be freely
declared in another class.
15
 Ask and guide me at
sunmitraeducation@gmail.com
 Share this information with as
many people as possible.
 Keep visiting www.sunmitra.com
for programme updates.

More Related Content

PPTX
Programming Primer EncapsulationVB
PPTX
Building high productivity applications
PDF
Does your code spark joy? Refactoring techniques to make your life easier.
PPT
Patterns in PHP
PDF
Functions in php
PDF
OOP in PHP
PDF
Unison Language - Contact
PDF
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute
Programming Primer EncapsulationVB
Building high productivity applications
Does your code spark joy? Refactoring techniques to make your life easier.
Patterns in PHP
Functions in php
OOP in PHP
Unison Language - Contact
MVC Design Pattern in JavaScript by ADMEC Multimedia Institute

What's hot (20)

PPT
Aggregate functions
PDF
Test and API-driven development of CakePHP Behaviors
ODP
Object Oriented Programming With PHP 5 #2
PPT
Comp102 lec 4
PDF
Functional Effects - Part 1
PDF
De constructed-module
ODP
Automated Refactoring
PDF
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
PDF
Monad Fact #6
KEY
Dsl
PDF
Factory Girl
PDF
Paying off technical debt with PHPSpec
PDF
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
PPTX
Objective-c Runtime
PDF
Functional Effects - Part 2
PDF
How to get along with implicits
PPTX
Builder pattern
PDF
Functional Javascript
PPTX
重構—改善既有程式的設計(chapter 9)
PPTX
Encapsulation
Aggregate functions
Test and API-driven development of CakePHP Behaviors
Object Oriented Programming With PHP 5 #2
Comp102 lec 4
Functional Effects - Part 1
De constructed-module
Automated Refactoring
Diffing Shotgun Surgery and Divergent Change smells in the two editions of Re...
Monad Fact #6
Dsl
Factory Girl
Paying off technical debt with PHPSpec
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Objective-c Runtime
Functional Effects - Part 2
How to get along with implicits
Builder pattern
Functional Javascript
重構—改善既有程式的設計(chapter 9)
Encapsulation
Ad

Similar to Programming Primer Encapsulation CS (20)

PPTX
Object Oriented Programming - Abstraction & Encapsulation
PPTX
Module 4 Effect of Reuse on using Encapsulation.pptx
DOCX
Benefits of encapsulation
PPTX
Prese00yq3whesfthewgdsyuvferwyjhjdfegcyjgfz.pptx
PPTX
Access Modifiers in C# ,Inheritance and Encapsulation
PPTX
Presentation related to Encapsulation and OOPs
PPTX
Presen5416846534653416354165341864adeadvdes
PPTX
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
PPTX
Presentation - Copy no vaperpoit asd.pptx
PPTX
Encapsulation
PDF
Lecture09a computer applicationsie1_dratifshahzad
PPTX
oop ppt.pptxfwefwefweqwedrqwerwerweewrewe
PPT
Basic c#
PPTX
SAD05 - Encapsulation
PPTX
Encapsulation C++ Piller of OOP it is the important piller
PPTX
PPTX
27csharp
PPTX
20.3 Java encapsulation
DOCX
Introduction to object oriented programming concepts
PPS
Aae oop xp_03
Object Oriented Programming - Abstraction & Encapsulation
Module 4 Effect of Reuse on using Encapsulation.pptx
Benefits of encapsulation
Prese00yq3whesfthewgdsyuvferwyjhjdfegcyjgfz.pptx
Access Modifiers in C# ,Inheritance and Encapsulation
Presentation related to Encapsulation and OOPs
Presen5416846534653416354165341864adeadvdes
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
Presentation - Copy no vaperpoit asd.pptx
Encapsulation
Lecture09a computer applicationsie1_dratifshahzad
oop ppt.pptxfwefwefweqwedrqwerwerweewrewe
Basic c#
SAD05 - Encapsulation
Encapsulation C++ Piller of OOP it is the important piller
27csharp
20.3 Java encapsulation
Introduction to object oriented programming concepts
Aae oop xp_03
Ad

More from sunmitraeducation (20)

PPTX
Java Introduction
PPTX
Installing JDK and first java program
PPTX
Project1 VB
PPTX
Project1 CS
PPTX
Grid Vew Control VB
PPTX
Grid View Control CS
PPTX
PPTX
Database Basics Theory
PPTX
Visual Web Developer and Web Controls VB set 3
PPTX
Visual Web Developer and Web Controls CS set 3
PPTX
Progamming Primer Polymorphism (Method Overloading) VB
PPTX
Programming Primer Inheritance VB
PPTX
Programming Primer Inheritance CS
PPTX
ProgrammingPrimerAndOOPS
PPTX
Web Server Controls VB Set 1
PPTX
Web Server Controls CS Set
PPTX
Web Controls Set-1
PPTX
Understanding IDEs
PPTX
Html Server Image Control VB
PPTX
Html Server Image Control CS
Java Introduction
Installing JDK and first java program
Project1 VB
Project1 CS
Grid Vew Control VB
Grid View Control CS
Database Basics Theory
Visual Web Developer and Web Controls VB set 3
Visual Web Developer and Web Controls CS set 3
Progamming Primer Polymorphism (Method Overloading) VB
Programming Primer Inheritance VB
Programming Primer Inheritance CS
ProgrammingPrimerAndOOPS
Web Server Controls VB Set 1
Web Server Controls CS Set
Web Controls Set-1
Understanding IDEs
Html Server Image Control VB
Html Server Image Control CS

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Approach and Philosophy of On baking technology
PPT
Teaching material agriculture food technology
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Cloud computing and distributed systems.
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Approach and Philosophy of On baking technology
Teaching material agriculture food technology
Encapsulation theory and applications.pdf
NewMind AI Weekly Chronicles - August'25-Week II
sap open course for s4hana steps from ECC to s4
Digital-Transformation-Roadmap-for-Companies.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Building Integrated photovoltaic BIPV_UPV.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
MIND Revenue Release Quarter 2 2025 Press Release
Cloud computing and distributed systems.
Chapter 3 Spatial Domain Image Processing.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine Learning_overview_presentation.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx

Programming Primer Encapsulation CS

  • 1. 1 A Programme Under the compumitra Series Programming Primer: Encapsulation and Abstraction LAB WORK GUIDE
  • 2. 2 OUTLINE  Encapsulation and Abstraction Using C# in asp.net Creating Example Event Handler. Example Output an Explanation. Home Exercise. Summary Review.
  • 4. 4 EncapsulationCS – Creating Button and Label Template Button with 'Text' Property set to 'Encapsulation'. Two Labels to hold the place for output display with 'text' property set to NULL (blank). • Follow Standard Website Creation Steps from C# and set your path to C:Learner<student-id>ProgrammingPrimerEncapsulationCS • Now Create the Execution Event Handler as a button and output place holders as follows.
  • 5. 5 EncapsulationCS –Copy and Paste Code Go to 'Default.aspx.cs' and 'Paste' the Code in 'Button1_Click' handler. Copy this Code Employee emp = new Employee(); emp.EmployeeID = 100; emp.Salary = 8500.30; Label1.Text = "Employee ID = " + emp.EmployeeID.ToString(); Label2.Text = "Employee Salary = " + emp.Salary.ToString(); Employee emp = new Employee(); emp.EmployeeID = 100; emp.Salary = 8500.30; Label1.Text = "Employee ID = " + emp.EmployeeID.ToString() ; Label2.Text = "Employee Salary = " + emp.Salary.ToString() ;
  • 6. 6 EncapsulationCS –Copy Code class Employee { private int _employeeID; private double _salary; public int EmployeeID //Properties { set { _employeeID = value; } get { return _employeeID; } } public double Salary //Properties { set { _salary = value; } get { return _salary; } } } Copy this Code
  • 7. 7 EncapsulationCS –Paste Code Run Code By pressing 'F5' Paste code after the End of '_Default' class class Employee { private int _employeeID; private double _salary; public int EmployeeID //Properties { set { _employeeID = value; } get { return _employeeID; } } public double Salary //Properties { set { _salary = value; } get { return _salary; } } } Label1.Text = emp.EmployeeID.ToString(); Label2.Text = emp.Salary.ToString();
  • 8. 8 Employee emp = new Employee(); emp.EmployeeID = 100; emp.Salary = 8500.30; Label1.Text = emp.EmployeeID.ToString(); Label2.Text = emp.Salary.ToString(); EncapsulationCS –Output Output after executing the handler using the button. This 'output' is generated, because we are trying to fill values in public properties that are accessible outside class even though the values are actually filled in private values such as ' _salary ' '_employeeID'. We are able to change this as this is defined as 'public' in original class. Don't worry we shall soon learn 'private' declaration too.
  • 9. 9 class Employee { private int _employeeID; private double _salary; public int EmployeeID //Properties { set { _employeeID = value; } get { return _employeeID; } } public double Salary //Properties { set { _salary = value; } get { return _salary; } } } EncapsulationCS – Example Explanation This has been defined as 'Private'. This means that only code written inside class Employee can modify it. This is 'Public' as we would like to use a way to change this property from an object based on class Employee. 'Public' and 'Private' declarations are known as access modifiers and they provide the primary technique of ENCAPSULATION. Are there other such modifiers? Surely Yes.
  • 10. 10 class Employee { private int _employeeID; private double _salary; public int EmployeeID //Properties { set { _employeeID = value; } get { return _employeeID; } } public double Salary //Properties { set { _salary = value; } get { return _salary; } } } EncapsulationCS – Example Explanation for ABSTRACTION Lets Focus on this class 'Employee'. Suppose I have to define an additional property 'employ_name' then it is most likely that I shall define it within this class. Means to external world 'Employee' is an abstract representation of all behaviours(methods) and states(properties) of employee and it is supposed to encapsulate everything related to this class. So! Encapsulation helps to attain ABSTRACTION. Abstraction also means that class is just a template or a map. Actual embodiment or physical usage is where we create an object based on a class.
  • 11. 11 emp._employeeID = 100; emp._salary = 8500.30; Label1.Text = "Employee ID = " + emp._employeeID.ToString(); Label2.Text = "Employee Salary = " + emp._employeeID.ToString(); emp._employeeID = 100; emp._salary = 8500.30; Label1.Text = "Employee ID = " + emp._employeeID.ToString(); Label2.Text = "Employee Salary = " + emp._employeeID.ToString(); EncapsulationCS – Error Trial Make necessary changes here and 'Run' code. Here we shall try to use the same code with a change to try using 'Private' variable _employeeID and _salary.
  • 12. 12 EncapsulationCS–Output after changing code This program will generate a 'Compiler Error Message: CS0122: 'Employee._employeeID' is not accessible as it is 'Private'. Here we see that variable '_employeeID' and '_salary' is actually hidden behind the class ' Employee'. We can not initialize out side the class by using object 'emp' of class 'Employee'. This Error Output Actually proves that the concept of 'ENCAPSULATION' works. Code in Error but we are still very happy.
  • 13. 13 EncapsulationCS : Home Exercise  You are required to make a program where you can declare a class 'Mobile'.  Make a public method sendsms( ).  Make a protected method typetext( ).  Call typetext( ) within sendsms( ).  Now write some eventhandler to show functionality if protected could be called outside class or not.  Now further create a class derived from class 'mobile' and name it as 'iphone'.  Try to use the typetext( ) method now. We have told you about 'Private' and 'Public' access modifiers. Another common access modifier used is 'Protected' A 'Protected' access modifier allows access within a derived (child) class, but not outside class. Are there other access modifiers. Try to search internet to find-out and understand. Difficult?. 'Facing Difficulties is the prime goal of a programmer'. Play some music and relax. Next is just a summary slide.
  • 14. 14 EncapsulationCS : Learning Summary Review  Concept of Encapsulation  Encapsulation helps to provide controlled access to outside classes.  Encapsulation also helps to bundle similar things together.  Concept of Abstraction  Encapsulation helps to attain Abstraction.  Abstraction also relates to non-physical nature of classes.  Trying to use a 'Private' data outside the class will give an error. For this purpose 'Public' declaration should be done.  A 'Protected' modifier is suitable for neat definition of access restriction for access to derived classes only.  Private properties or methods also serve an additional purpose. If a property or method is private in a class means the property or methods of same name can be freely declared in another class.
  • 15. 15  Ask and guide me at sunmitraeducation@gmail.com  Share this information with as many people as possible.  Keep visiting www.sunmitra.com for programme updates.