Skip to main content

Encapsulation

Most of you heard the word 'Encapsulation' while dealing with Java OOPs concepts. What it stands for?

We will start with an example. Imagine you have made your class with public instance variables as shown below.

public class Sample{
public int height;
public int weight;
}
public class MainClass{
public static void main(String args[]){
Sample sample = new Sample();
sample.height = 170;
}


You have created this class and think that many of others used this class for setting height and weight variables directly. Now you are really in trouble. Just think a situation that you want to do something when someone set height variable?. Can you do that without breaking everyone's code?

Here comes encapsulation. It helps to change your implemented code without breaking the code of others who use your code. You must follow encapsulation if you want maintainability , flexibility and extensibility.

  • Instance variables must be private or protected.
  • To change an instance variable , use public accessor methods , and force others to use this
    methods to change the variable.
The above example can be changed to include encapsulation

public class
Sample{
private int height;

private int weight;

public void setHeight(int height){
this.height = height;
}

public void setWeight(int weight){
this.weight= weight;
}

public int getHeight(){
return height;'
}

public int getWeight(){
return weight;'
}
}

public class MainClass{
public static void main(String args[]){
Sample sample = new Sample();
sample.setHeight(170); // User forced to use the public method to set value
}


Hope you got an idea about encapsulation... :)




Comments

Popular posts from this blog

Initializing fields to 0-false-null is redundant

One of the most fundamental aspects of a programming language is how it initializes data. For Java, this is defined explicitly in the language . For fields and array components , when items are created, they are automatically set to the following default values by the system: numbers: 0 or 0.0 booleans: false object references: null This means that explicitly setting fields to 0, false, or null (as the case may be) is unnecessary and redundant. Since this language feature was included in order to, in part, reduce repetitive coding, it's a good idea to take full advantage of it. Insisting that fields should be explicitly initialized to 0, false, or null is an idiom which is likely inappropriate to the Java programming language. _______________________________________________

One good Question about Garbage Collection

Class CardBoard {        Short story = 200;        CardBoard go(CardBoard cb){             cb = null;             return cb;        }        public static void main(String args[]){             CardBoard c1 = new CardBoard();             CardBoard c2 = new CardBoard();             CardBoard c3 = c1.go(c2);             c1 = null; // (1)            // some code        } } After executing line (1) how many objects are eligible for garbage collection ? First we can go through the code, - We are creating two objects and they are referred by reference variables c1 and c2.- During the execution of thisline CardBoard c3 = c1.go(c2); no new objects are created in the heap. - The...

Explain the Class Loader Mechanism of JVM?. How to write a custom Class Loader Class?

Every java program has at least three class loaders. - The bootstrap class loader - The extension class loader. - The system class loader. The bootstrap class loader loads the system classes from the JAR Fle rt.jar(part of JRE System Library) . It is an integral part of the virtual machine and is usually implemented in C. There is no Class loader object corresponding to the bootstrap class loader. For example. String.class.getClassLoade r() will return null since String class is loader by bootstrap loader. The extension class loaders loads "standard extensions "from the jre/lib/ext directory. The system class loader loads the application classes. It locates the classes in the directories and JAR/ZIP files on the class path. The System class loader also called application class loader. In Sun's java implementation , the extension adn system class loaders are implemented in java. Both are instances of URLClass loader. You can just go through the source code of URLCla...