SlideShare una empresa de Scribd logo
1 de 15
1
A Programme Under the compumitra Series
Programming Primer:
Encapsulation and Abstraction
LAB WORK GUIDE
2
OUTLINE
 Encapsulation and Abstraction Using
VB in asp.net
Creating Example Event Handler.
Example Output an Explanation.
Home Exercise.
Summary Review.
3
Encapsulation and Abstraction
Using VB in asp.net
4
EncapsulationVB – 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 and set your path to
C:Learner<student-id>ProgrammingPrimerEncapsulationVB
• Now Create the Execution Event Handler as a button and output
place holders as follows.
5
EncapsulationVB –Copy and Paste Code
Go to 'Default.aspx.cs' and 'Paste' the Code in
'Button1_Click' handler.
Copy this Code
Dim emp As New Employee
emp.EmployeeID = 100
emp.Salary = 8500.35
Label1.Text = "Employee ID = " & emp.EmployeeID.ToString()
Label2.Text = "Employee Salary = " & emp.Salary.ToString()
Dim emp As New Employee
emp.EmployeeID = 100
emp.Salary = 8500.35
Label1.Text="Employee ID = " &
emp.EmployeeID.ToString()
Label2.Text="Employee Salary = " &
emp.Salary.ToString()
6
EncapsulationVB –Copy Code
Class Employee
Private _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class Copy this Code
7
EncapsulationVB –Paste Code
Run Code By
pressing 'F5'
Class Employee
Private _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class
Label1.Text = "Employee ID = " & emp.EmployeeID.ToString()
Label2.Text = "Employee Salary = " & emp.Salary.ToString()
Paste code after the End
of '_Default' class
8
Employee emp = new Employee();
emp.EmployeeID = 100;
emp.Salary = 8500.30;
Label1.Text = emp.EmployeeID.ToString();
Label2.Text = emp.Salary.ToString();
EncapsulationVB –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 _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class
EncapsulationVB – 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 _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class
EncapsulationVB – 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
Dim emp As New Employee
emp ._employeeID = 100
emp ._salary = 8500.3
Label1.Text = "Employee ID = " & emp ._employeeID.ToString()
Label2.Text = "Employee Salary = " & emp._salary.ToString()
Dim emp As New Employee
emp ._employeeID = 100
emp ._salary = 8500.3
Label1.Text = "Employee ID = " & emp ._employeeID.ToString()
Label2.Text = "Employee Salary = " & emp._salary.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
EncapsulationVB–Output after changing code
This program will generate a 'Compiler
Error Message: BC30390:
'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
EncapsulationVB : 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
EncapsulationVB : 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.

Más contenido relacionado

La actualidad más candente

Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
Manav Gupta
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
Alexe Bogdan
 

La actualidad más candente (20)

Variables, expressions, standard types
 Variables, expressions, standard types  Variables, expressions, standard types
Variables, expressions, standard types
 
Functional Effects - Part 1
Functional Effects - Part 1Functional Effects - Part 1
Functional Effects - Part 1
 
Monad Fact #6
Monad Fact #6Monad Fact #6
Monad Fact #6
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Functional Effects - Part 2
Functional Effects - Part 2Functional Effects - Part 2
Functional Effects - Part 2
 
Javascript best practices
Javascript best practicesJavascript best practices
Javascript best practices
 
iOS best practices
iOS best practicesiOS best practices
iOS best practices
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Angular custom directives
Angular custom directivesAngular custom directives
Angular custom directives
 
Only oop
Only oopOnly oop
Only oop
 
JavaScript Refactoring
JavaScript RefactoringJavaScript Refactoring
JavaScript Refactoring
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2
 
Unbreakable Domain Models PHPUK 2014 London
Unbreakable Domain Models PHPUK 2014 LondonUnbreakable Domain Models PHPUK 2014 London
Unbreakable Domain Models PHPUK 2014 London
 
Paying off technical debt with PHPSpec
Paying off technical debt with PHPSpecPaying off technical debt with PHPSpec
Paying off technical debt with PHPSpec
 
How to get along with implicits
How to get along with implicits How to get along with implicits
How to get along with implicits
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
Strings
StringsStrings
Strings
 
PHP-Part2
PHP-Part2PHP-Part2
PHP-Part2
 
Oop php 5
Oop php 5Oop php 5
Oop php 5
 
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
 

Similar a Programming Primer EncapsulationVB

Please be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docxPlease be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docx
lorindajamieson
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstraction
Raghav Chhabra
 
Use of Classes and functions#include iostreamusing name.docx
 Use of Classes and functions#include iostreamusing name.docx Use of Classes and functions#include iostreamusing name.docx
Use of Classes and functions#include iostreamusing name.docx
aryan532920
 
Cis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variablesCis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variables
cis247
 
Cis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variablesCis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variables
ccis224477
 
Cis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variablesCis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variables
sdjdskjd9097
 
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
gilbertkpeters11344
 
Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdf
adityastores21
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
dunhamadell
 
Cis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classCis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee class
sdjdskjd9097
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 

Similar a Programming Primer EncapsulationVB (20)

Python classes objects
Python classes objectsPython classes objects
Python classes objects
 
Please be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docxPlease be advised that there are four (4) programs just like this on.docx
Please be advised that there are four (4) programs just like this on.docx
 
Interface and abstraction
Interface and abstractionInterface and abstraction
Interface and abstraction
 
Python advance
Python advancePython advance
Python advance
 
Getting started with ES6
Getting started with ES6Getting started with ES6
Getting started with ES6
 
CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces  CIS 247C iLab 4 of 7: Composition and Class Interfaces
CIS 247C iLab 4 of 7: Composition and Class Interfaces
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
Use of Classes and functions#include iostreamusing name.docx
 Use of Classes and functions#include iostreamusing name.docx Use of Classes and functions#include iostreamusing name.docx
Use of Classes and functions#include iostreamusing name.docx
 
Cis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variablesCis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variables
 
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good PracticesEffective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good Practices
 
Cis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variablesCis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variables
 
Cis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variablesCis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variables
 
Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1 Problem solving using computers - Chapter 1
Problem solving using computers - Chapter 1
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
 
Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdf
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
 
Cis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classCis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee class
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 

Más de sunmitraeducation

Más de sunmitraeducation (20)

Java Introduction
Java IntroductionJava Introduction
Java Introduction
 
Installing JDK and first java program
Installing JDK and first java programInstalling JDK and first java program
Installing JDK and first java program
 
Project1 VB
Project1 VBProject1 VB
Project1 VB
 
Project1 CS
Project1 CSProject1 CS
Project1 CS
 
Grid Vew Control VB
Grid Vew Control VBGrid Vew Control VB
Grid Vew Control VB
 
Grid View Control CS
Grid View Control CSGrid View Control CS
Grid View Control CS
 
Ms Access
Ms AccessMs Access
Ms Access
 
Database Basics Theory
Database Basics TheoryDatabase Basics Theory
Database Basics Theory
 
Visual Web Developer and Web Controls VB set 3
Visual Web Developer and Web Controls VB set 3Visual Web Developer and Web Controls VB set 3
Visual Web Developer and Web Controls VB set 3
 
Visual Web Developer and Web Controls CS set 3
Visual Web Developer and Web Controls CS set 3Visual Web Developer and Web Controls CS set 3
Visual Web Developer and Web Controls CS set 3
 
Progamming Primer Polymorphism (Method Overloading) VB
Progamming Primer Polymorphism (Method Overloading) VBProgamming Primer Polymorphism (Method Overloading) VB
Progamming Primer Polymorphism (Method Overloading) VB
 
Programming Primer Inheritance VB
Programming Primer Inheritance VBProgramming Primer Inheritance VB
Programming Primer Inheritance VB
 
Programming Primer Inheritance CS
Programming Primer Inheritance CSProgramming Primer Inheritance CS
Programming Primer Inheritance CS
 
ProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPSProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPS
 
Web Server Controls VB Set 1
Web Server Controls VB Set 1Web Server Controls VB Set 1
Web Server Controls VB Set 1
 
Web Server Controls CS Set
Web Server Controls CS Set Web Server Controls CS Set
Web Server Controls CS Set
 
Web Controls Set-1
Web Controls Set-1Web Controls Set-1
Web Controls Set-1
 
Understanding IDEs
Understanding IDEsUnderstanding IDEs
Understanding IDEs
 
Html Server Image Control VB
Html Server Image Control VBHtml Server Image Control VB
Html Server Image Control VB
 
Html Server Image Control CS
Html Server Image Control CSHtml Server Image Control CS
Html Server Image Control CS
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

Programming Primer EncapsulationVB

  • 1. 1 A Programme Under the compumitra Series Programming Primer: Encapsulation and Abstraction LAB WORK GUIDE
  • 2. 2 OUTLINE  Encapsulation and Abstraction Using VB in asp.net Creating Example Event Handler. Example Output an Explanation. Home Exercise. Summary Review.
  • 4. 4 EncapsulationVB – 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 and set your path to C:Learner<student-id>ProgrammingPrimerEncapsulationVB • Now Create the Execution Event Handler as a button and output place holders as follows.
  • 5. 5 EncapsulationVB –Copy and Paste Code Go to 'Default.aspx.cs' and 'Paste' the Code in 'Button1_Click' handler. Copy this Code Dim emp As New Employee emp.EmployeeID = 100 emp.Salary = 8500.35 Label1.Text = "Employee ID = " & emp.EmployeeID.ToString() Label2.Text = "Employee Salary = " & emp.Salary.ToString() Dim emp As New Employee emp.EmployeeID = 100 emp.Salary = 8500.35 Label1.Text="Employee ID = " & emp.EmployeeID.ToString() Label2.Text="Employee Salary = " & emp.Salary.ToString()
  • 6. 6 EncapsulationVB –Copy Code Class Employee Private _employeeID As Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class Copy this Code
  • 7. 7 EncapsulationVB –Paste Code Run Code By pressing 'F5' Class Employee Private _employeeID As Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class Label1.Text = "Employee ID = " & emp.EmployeeID.ToString() Label2.Text = "Employee Salary = " & emp.Salary.ToString() Paste code after the End of '_Default' class
  • 8. 8 Employee emp = new Employee(); emp.EmployeeID = 100; emp.Salary = 8500.30; Label1.Text = emp.EmployeeID.ToString(); Label2.Text = emp.Salary.ToString(); EncapsulationVB –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 _employeeID As Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class EncapsulationVB – 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 _employeeID As Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class EncapsulationVB – 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 Dim emp As New Employee emp ._employeeID = 100 emp ._salary = 8500.3 Label1.Text = "Employee ID = " & emp ._employeeID.ToString() Label2.Text = "Employee Salary = " & emp._salary.ToString() Dim emp As New Employee emp ._employeeID = 100 emp ._salary = 8500.3 Label1.Text = "Employee ID = " & emp ._employeeID.ToString() Label2.Text = "Employee Salary = " & emp._salary.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 EncapsulationVB–Output after changing code This program will generate a 'Compiler Error Message: BC30390: '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 EncapsulationVB : 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 EncapsulationVB : 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.