SlideShare a Scribd company logo
1 of 13
Download to read offline
Chapter 3: Encapsulation
Data Hiding
•   1 of the important OOP techniques
•   Hiding the data within the class
•   Making it available ONLY through the methods
•   Also known as encapsulation
    ▫ It seals the data (and internal method) safely inside
      the “capsule” of the class
    ▫ Can only be accessed by a trusted user (i.e. by the
      method of the class)
Why need encapsulation?
• To hide the internal implementation details of the
  class
• Can safely modified the implementation without
  worrying breaking the existing code that uses the
  class
• Protect class against accidental/ willful stupidity
• Keeps class tidy by keeping the visible fields to a
  minimum
• Easier to use and understand
Access Control
• All the fields and methods of a class can always be
  used within the body of the class itself.
• Java defines access control rules that restrict
  members of a class from being used outside the
  class.
• The public keyword, along with protected and
  private, are accesscontrolmodifiers ; they specify
  the access rules for the field or method.
Access to packages
• A package is always accessible to code defined within
  the package.
• Whether it is accessible to code from other packages
  depends on the way the package is deployed on the host
  system.
• When the class files that comprise a package are stored
  in a directory, for example, a user must have read access
  to the directory and the files within it in order to have
  access to the package.
• Package access is not part of the Java language itself.
• Access control is usually done at the level of classes and
  members of classes instead
Access to classes
• By default, top-level classes are accessible within
  the package in which they are defined.
• However, if a top-level class is declared public, it
  is accessible everywhere (or everywhere that the
  package itself is accessible).
Access to members
• By default, members are also accessible throughout
  the package in which the class is defined.
• This default level of access is often called
  packageaccess.
• The other three levels of access are defined by the
  public, protected, and private modifiers.
example
// People can use this class.
public class Laundromat {

// They cannot use this internal field,
private Laundry[] dirty;

// but they can use these public methods
public void wash() { ... }

// to manipulate the internal field.
public void dry() { ... }
}
Access rules:
• If a member of a class is declared with the public modifier,
  it means that the member is accessible anywhere the
  containing class is accessible. This is the least restrictive type
  of access control.
• If a member of a class is declared private, the member is
  never accessible, except within the class itself. This is the
  most restrictive type of access control.
• If a member of a class is declared protected, it is
  accessible to all classes within the package (the same as the
  default package accessibility) and also accessible within the
  body of any subclass of the class, regardless of the package
  in which that subclass is defined. This is more restrictive than
  public access, but less restrictive than package access.
• If a member of a class is not declared with any of these
  modifiers, it has the default package access: it is accessible to
  code within all classes that are defined in the same package,
  but inaccessible outside of the package.
Another example
                  The public methods are
                  the access points to this
                  class.s fields from the outside
                  java world.

                  Normally these methods are
                  referred as getters and
                  setters.

                  Therefore any class that
                  wants to access the variables
                  should access them through
                  these getters and setters.
~continued
The variables of the EncapTest class can be access as below:




                                         Output:
Benefits of having 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 fields, and a
  users of the class do not need to change any of their
  code
Question & answer

More Related Content

What's hot (13)

27csharp
27csharp27csharp
27csharp
 
Chapter 05 polymorphism extra
Chapter 05 polymorphism extraChapter 05 polymorphism extra
Chapter 05 polymorphism extra
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
Inner class
Inner classInner class
Inner class
 
Chap02
Chap02Chap02
Chap02
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
 
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
 
Inner classes
Inner classesInner classes
Inner classes
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
 
Inner Classes
Inner ClassesInner Classes
Inner Classes
 

Similar to Chapter 03 enscapsulation

Similar to Chapter 03 enscapsulation (20)

Oops (inheritance&interface)
Oops (inheritance&interface)Oops (inheritance&interface)
Oops (inheritance&interface)
 
Chap1 packages
Chap1 packagesChap1 packages
Chap1 packages
 
27c
27c27c
27c
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
 
Chapter 7 & 8 Classes in Java and Functions.pdf
Chapter 7 & 8 Classes in Java and Functions.pdfChapter 7 & 8 Classes in Java and Functions.pdf
Chapter 7 & 8 Classes in Java and Functions.pdf
 
Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
116824015 java-j2 ee
116824015 java-j2 ee116824015 java-j2 ee
116824015 java-j2 ee
 
Unit 3
Unit 3Unit 3
Unit 3
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Unit-4 Day1.pptx
Unit-4 Day1.pptxUnit-4 Day1.pptx
Unit-4 Day1.pptx
 
Javasession8
Javasession8Javasession8
Javasession8
 
Packages
PackagesPackages
Packages
 

Recently uploaded

Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
mountabuangels4u
 
sample sample sample sample sample sample
sample sample sample sample sample samplesample sample sample sample sample sample
sample sample sample sample sample sample
Casey Keith
 
bhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girls
bhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girlsbhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girls
bhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girls
mountabuangels4u
 
IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM111IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM111
2022472524
 

Recently uploaded (20)

Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
 
Kashipur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Kashipur Call Girls 🥰 8617370543 Service Offer VIP Hot ModelKashipur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Kashipur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
sample sample sample sample sample sample
sample sample sample sample sample samplesample sample sample sample sample sample
sample sample sample sample sample sample
 
Raiganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Raiganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelRaiganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Raiganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Discover Mathura And Vrindavan A Spritual Journey.pdf
Discover Mathura And Vrindavan A Spritual Journey.pdfDiscover Mathura And Vrindavan A Spritual Journey.pdf
Discover Mathura And Vrindavan A Spritual Journey.pdf
 
Rudrapur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Rudrapur Call Girls 🥰 8617370543 Service Offer VIP Hot ModelRudrapur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Rudrapur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
bhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girls
bhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girlsbhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girls
bhachau Escort💋 Call Girl (Ramya) Service #bhachau Call Girl @Independent Girls
 
Pithoragarh Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Pithoragarh Call Girls 🥰 8617370543 Service Offer VIP Hot ModelPithoragarh Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Pithoragarh Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Sun World Bana Hills, Vienam Part 2 (越南 巴拿山太陽世界 下集).ppsx
Sun World Bana Hills, Vienam Part 2  (越南 巴拿山太陽世界 下集).ppsxSun World Bana Hills, Vienam Part 2  (越南 巴拿山太陽世界 下集).ppsx
Sun World Bana Hills, Vienam Part 2 (越南 巴拿山太陽世界 下集).ppsx
 
Birbhum Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Birbhum Call Girls 🥰 8617370543 Service Offer VIP Hot ModelBirbhum Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Birbhum Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A high-altitude adventure in the mountain kingdom
A high-altitude adventure in the mountain kingdomA high-altitude adventure in the mountain kingdom
A high-altitude adventure in the mountain kingdom
 
TOURISM ATTRACTION IN LESOTHO 2024.pptx.
TOURISM ATTRACTION IN LESOTHO 2024.pptx.TOURISM ATTRACTION IN LESOTHO 2024.pptx.
TOURISM ATTRACTION IN LESOTHO 2024.pptx.
 
IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM111IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM111
 
Elevate Your Busy Season Email Marketing, Holly May Webinar.pptx
Elevate Your Busy Season Email Marketing, Holly May Webinar.pptxElevate Your Busy Season Email Marketing, Holly May Webinar.pptx
Elevate Your Busy Season Email Marketing, Holly May Webinar.pptx
 
Udhampur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Udhampur Call Girls 🥰 8617370543 Service Offer VIP Hot ModelUdhampur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Udhampur Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
abortion pills in Riyadh+966572737505 Cytotec Riyadh
abortion pills in  Riyadh+966572737505    Cytotec Riyadhabortion pills in  Riyadh+966572737505    Cytotec Riyadh
abortion pills in Riyadh+966572737505 Cytotec Riyadh
 
Top places to visit, top tourist destinations
Top places to visit, top tourist destinationsTop places to visit, top tourist destinations
Top places to visit, top tourist destinations
 
Top travel agency in panchkula - Best travel agents in panchkula
Top  travel agency in panchkula - Best travel agents in panchkulaTop  travel agency in panchkula - Best travel agents in panchkula
Top travel agency in panchkula - Best travel agents in panchkula
 
Ramnagar Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Ramnagar Call Girls 🥰 8617370543 Service Offer VIP Hot ModelRamnagar Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Ramnagar Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Overview of Lesotho 's natural beauty tourist attractions
Overview of Lesotho 's natural beauty tourist attractionsOverview of Lesotho 's natural beauty tourist attractions
Overview of Lesotho 's natural beauty tourist attractions
 

Chapter 03 enscapsulation

  • 2. Data Hiding • 1 of the important OOP techniques • Hiding the data within the class • Making it available ONLY through the methods • Also known as encapsulation ▫ It seals the data (and internal method) safely inside the “capsule” of the class ▫ Can only be accessed by a trusted user (i.e. by the method of the class)
  • 3. Why need encapsulation? • To hide the internal implementation details of the class • Can safely modified the implementation without worrying breaking the existing code that uses the class • Protect class against accidental/ willful stupidity • Keeps class tidy by keeping the visible fields to a minimum • Easier to use and understand
  • 4. Access Control • All the fields and methods of a class can always be used within the body of the class itself. • Java defines access control rules that restrict members of a class from being used outside the class. • The public keyword, along with protected and private, are accesscontrolmodifiers ; they specify the access rules for the field or method.
  • 5. Access to packages • A package is always accessible to code defined within the package. • Whether it is accessible to code from other packages depends on the way the package is deployed on the host system. • When the class files that comprise a package are stored in a directory, for example, a user must have read access to the directory and the files within it in order to have access to the package. • Package access is not part of the Java language itself. • Access control is usually done at the level of classes and members of classes instead
  • 6. Access to classes • By default, top-level classes are accessible within the package in which they are defined. • However, if a top-level class is declared public, it is accessible everywhere (or everywhere that the package itself is accessible).
  • 7. Access to members • By default, members are also accessible throughout the package in which the class is defined. • This default level of access is often called packageaccess. • The other three levels of access are defined by the public, protected, and private modifiers.
  • 8. example // People can use this class. public class Laundromat { // They cannot use this internal field, private Laundry[] dirty; // but they can use these public methods public void wash() { ... } // to manipulate the internal field. public void dry() { ... } }
  • 9. Access rules: • If a member of a class is declared with the public modifier, it means that the member is accessible anywhere the containing class is accessible. This is the least restrictive type of access control. • If a member of a class is declared private, the member is never accessible, except within the class itself. This is the most restrictive type of access control. • If a member of a class is declared protected, it is accessible to all classes within the package (the same as the default package accessibility) and also accessible within the body of any subclass of the class, regardless of the package in which that subclass is defined. This is more restrictive than public access, but less restrictive than package access. • If a member of a class is not declared with any of these modifiers, it has the default package access: it is accessible to code within all classes that are defined in the same package, but inaccessible outside of the package.
  • 10. Another example The public methods are the access points to this class.s fields from the outside java world. Normally these methods are referred as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.
  • 11. ~continued The variables of the EncapTest class can be access as below: Output:
  • 12. Benefits of having 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 fields, and a users of the class do not need to change any of their code