• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Encapsulation in JAVA

 
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the way other than Access modifiers to implement Encapsulation in Java?
 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aneek Banerjee wrote:What is the way other than Access modifiers to implement Encapsulation in Java?



As far as I know, encapsulation can be implemented only by using private access modifier.
 
Aneek Banerjee
Ranch Hand
Posts: 61
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets say if I make the instance variables final.Will it be count as Encapsulation?
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aneek Banerjee wrote:Lets say if I make the instance variables final.Will it be count as Encapsulation?



No, final does not count as encapsulation. Consider the following Example:

though t cannot hold reference to any other object as it's final, but the state of test object that it is referencing to can be changed by any class as it's marked public and not private.
 
Author
Posts: 63
6
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to see how one can implement encapsulation in Java, let us see design smells associated with encapsulation. We identified three smells associated with encapsulation:

1. Lenient Encapsulation
This smell occurs when the declared accessibility of one or more members of an abstraction is more permissive than actually required.

For example, Rectangle class exposes its data members and hence Rectangle class from JDK suffers from Lenient encapsulation smell.


2. Missing Encapsulation
This smell occurs when there exists global state (in the form of global variables, global data-structures, etc.) accessible to all abstractions in the software system.

For example, consider the following members defined in java.lang.System class:

Since they are declared coderanch, any code (i.e., other classes in JDK as well as application code) can access these members directly. Hence, these members are effectively global variables!

3. Unexploited Encapsulation
This smell arises when client code uses explicit type checks (using chained if-else or switch statements) instead of exploiting the variation in types encapsulated within a hierarchy.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic