Skip to main content

Encapsulation in Practical.

Encapsulation is one of the four fundamental OOP concepts.Encapsulation is the technique  to Provide the ability to a program to hind their internal characteristics and behaviorof by  creating
private fields in class and providing access to them via public methods.
If a field is declared private, it cannot be accessed by anyone outside the class,
thereby hiding the fields within the class.So encapsulation  protect them by unauthorized access / party  by creating a protecting wrapper around your code and data.

Encapsulation can be described as a protective barrier that prevents the code and data being
randomly accessed by other code defined outside the class.
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.

For Example

We have a data class with two fields name and id

public class Result{

   private String name;
   private String remark;


   public String getName(){
      return name;
   }
  public void setName(String newName){
      name = newName;
   }
   public String getRemark(){
      return remark;
   }

   public void setRemark( int remarkPoints){
     
      if(remarkPoints>=5)
      {
         remark="good"
      }

      if(remarkPoints<5)
      {
         remark="poor"
      }
     
   }

In above example we set remarks according to remark points.But after sometime we need to add one
more remark "very good" for student who gets 10 points so we wont ask user of code to change but we will change the setter


 public void setRemark( int remarkPoints){
   
      if(remarkPoints==10)
      {
         remark="very good"
      }

      if(remarkPoints>=5 && remarkPoints<10)
      {
         remark="good"
      }

      if(remarkPoints<5)
      {
         remark="poor"
      }
     
   }


More Example :-


Like we have a class Phone with filed weight, if you will not encapsulate it someone can modify it to have –ve weight,
so it is better to encapsulate using private variable and providing setter method where you will add logic of No –ve and Not over 100 grams.


Let’s take real example of CAR, car is an well-defined composite object with lots of sub systems like transmission system,
music system, air-cooler  or heater system and may have other based on the price of the car.
Now each system has their own protecting layer and because of that transmission system is not interfering with music system
or air-cooler system but yes there is some integration such a way that transmission charges the battery and it helps to play music but no one can directly modify the behavior of other system. So this is the way they hides the complexity of a program



Benefits of Encapsulation:
1)The fields of a class can be made read-only or write-only.

2)A class can have total control over what is stored in its fields.

3)The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code because
users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.

Popular posts from this blog

What are Java heap Young, Old and Permanent Generations?

The Java heap is dived into two parts i.e. Young generation and old generation. Young generationYoung generation  memory is 40% of Maximum Java Heap. It consists of two parts, Eden space and Survivor Space. Eden SpaceInitially objects are created in this  Part of Java Heap , here most objects die and quickly are cleaned up by the minor Garbage Collectors (It only runs in young generation and its frequency is high in compared to major garbage collector). Usually any new objects created inside a Java Method go into Eden space and the objects space is reclaimed once the method execution completes. Whereas the Instance Variables of a Class usually lives longer until the Object based on that class gets destroyed. When Eden fills up it causes a minor collection, in which some surviving objects are moved to Survivor Space or an older generation. Survivor Space  The pool containing objects that have survived the garbage collection of the Eden space The parameter Survivor Ratio can be used to tune…

Compression of HttpServletRequest and HttpServletResponse by gzip encoding

Description 
This filter is on HTTP headers in a HttpServletRequest, compress data written to the HttpServletResponse, or decompress data read from the request. When supported by the client browser, this can potentially greatly reduce the number of bytes written across the network from and to the client. As a Filter, this class can also be easily added to any J2EE 1.3+ web application.





Installation

1).Add the pjl-comp-filter-XX.jar file containing CompressingFilter to your web application's WEB-INF/lib directory.

2).Add the following entries to your web.xml deployment descriptor:

 <filter>
  <filter-name>CompressingFilter</filter-name>
  <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>CompressingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

The below data is of my application where i tested this compression technique…

4 mains causes of Long Garbage Collection Pause?

So a long GC Pause can have a direct impact on response time and throughput. But there are some causes for long GC pause which you can check and avoid to increase performance. They are as follows:-


1)Insufficient heap size:- If heap size is small then the size required then the allocation requests for new objects  fail and the JVM needs to invoke garbage collections in an attempt to reclaim space for the allocations. So these frequent Full GCs cause long pauses in the application run. To avoid long GC pause by this identify the average footprint of the application and then specify the heap size accordingly.
2)Fragmentation in the heap:-GCs can occur more frequently due to fragmentation in the Java Heap  and also sometimes causing long pauses in the GCs. This is more probable in the case of Concurrent Mark Sweep collector, also known as CMS, where the tenured generation space is not compacted with the concurrent collections. For more please go through the previous post about heap  compact…