Sharing Knlowledge With The World…

Tag: Java

Encapsulation in object-oriented language

Encapsulation

Encapsulation in object-oriented language or in Java is packing of data and function in to single component which enforce protecting variables, functions from outside of class, in order to better manage that piece of code and having least impact or no impact on other parts of program due to change in protected code.
Encapsulation in object-oriented language can also be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface. (Through functions, which are exposed to outer world.)
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.
Example:
public class UserPin {
private int pin;
public void setPin (int pin){
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from db and return back
}
}

Encapsulation in object-oriented language makes sure that the user of the class would be unaware of how class stores its data. Also it makes sure that user of the class do no need to change any of their code if there is any change in the class.
As in the above code example we store the ‘PIN’ of the user as integer but say, due to security reason we have to encrypt the ‘PIN’ and then store the encrypted ‘PIN’. And the algorithm that we use for encryption requires ‘PIN’ as String.
public class UserPin {
private int pin;
public void setPin (int pin){
//Convertin pin from int to String
//Encrytpt the PIN
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from database
//Converting back to int
//Returning the pin

}
}
As we saw there is no change in the signature of the functions so the user of the class do not have to change his code.
Also we can implement the security layer as the user access the field through the function (known as getter and setter).
public class UserPin {
private int pin;
public void setPin (int pin){
//Validate the value of the PIN
//Convertin pin from int to String
//Encrytpt the PIN
//Saving the pin to database
}
public int getPIn() {
//fetching the pin from database
//Converting back to int
//Returning the pin

}
}
The fields can be made read-only (If we don’t define setter methods in the class) or write-only (If we don’t define the getter methods in the class).

The whole idea behind encapsulation is to hide the implementation details from users. That’s why encapsulation is known as data hiding.

The idea of encapsulation in object-oriented language is “don’t tell me how you do it; just do it.”

Continue Reading

Interface in Object-oriented Programming

Interface in Object-oriented Programming

Objects interact with outside word through the methods that they exposed.

So Interface can be defined as contract which has enlisted all the methods which must be supported by the objects.

One Liner: An Interface in Object-oriented Programming is a blueprint of a class. It has static constants and abstract methods.

The interface is a mechanism to achieve fully abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritances in Java.

Consider ‘receive’ and ‘reject’ buttons on your phone. You know that these buttons will receive and reject the call and must support by the phone that’s why they are in the phone. The buttons acts as interface between you and phone.

Interface Phone {

Void receives ()

Void reject ();

            }

Interfaces cannot be instantiated, but rather are implemented.Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

In Java IS-A relationship is represented by interface.

Interface in Object-oriented Programming

Interface in Object-oriented Programming

 

Continue Reading

Class in Object-oriented Programming

Class in Object-oriented Programming

Class in object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions, methods).

 

One Liner: A Class in Object-oriented Programming is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

A class is a blueprint or template or set of instructions to build a specific type of object. Every object is built from a class.

Class in Object-oriented Programming

Class in Object-oriented Programming

Let’s try to understand the class with the example of making cookies.

Class is like recipe of how to make cookie. The recipe itself is not a cookie. You can’t eat the recipe. If you follow the recipe then you can make cookie. The cookie which we made from the recipe can be referred as an object.

You can make as many cookies as you would like using the same recipe. Similarly you can create as many instances of a class as you would like.

Now assume that you have to create cookies for different people. How you will identify which cookie is for which person. A simple solution is that write name of the person on the cookie. Reference variables work in similar fashion. A reference variable provides a unique name for each instance of a class. In order to work with a particular instance, you use the reference variable it is assigned to.

Creating class Cookie:

Class Cookie {

String flavor;

String taste;

}

 

Creating cookies for different persons:

Cookie cookieForPerson1 = new Cookie ();

Cookie cookieForPerson2 = new Cookie ();

 

 

Continue Reading

Package in JAVA

JAVA

Suppose you and your friend are freelancers and work as a team. You got a project which has to complete in one week. So you decided to divide the project in two modules and each of you works on different module. In this way you can complete the project in time. Say the name of the module on which you working is ‘module-A’ and the module on which on your friend is working is ‘module-B’.

Now both of the modules have the class name ‘Utility’.

When you combine both the module at the last then you have two ‘Utility’ classes. Now how you would identify which ‘Utility’ class belong to whom. Package is the solution for these types of problems.

Both of you write your code in your package and the both the ‘Utility’ classes belong to different packages, so there is no ambiguity.

One Liner: A Package in JAVA is a namespace that organizes a set of related classes and interfaces.

You can relate packages as being similar to the folders on your computer. You can put images in one folder, application in another and movies in another. Software written in Java programming language can be composed of thousands of classes so it is good practice to organize our packages and classes in by putting then in different packages.

When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.

 

Example of Package in JAVA:

 

package car;

 interface Car {

   public void run();   public void break();

}

 

package car; 

public class Honda implements Car {  

public void run(){}  

public void break(){}

}

 

Continue Reading

Garbage Collection in JAVA

Garbage Collection in JAVA

 One of the finest achievement of the JAVA is Garbage Collection. Now developers not to worry about allocation and de-allocation of the memory and create new objects. The responsibility of reclaiming the memory is of Garbage Collector.

One Liner: Garbage Collection is the mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage Collection.

In order to understand how Garbage Collection works in java, first we need to understand how memory is managed in Java Virtual Machine (JVM).

Heap Memory

When a JAVA programme starts, OS allocate some memory to JVM. JVM uses this memory for its need and use a part of this memory as Heap Memory.

Heap Memory is the area of the memory used for dynamic allocation. Generally Heap is located at the bottom of address space and move upwards.

 

Memory Allocation in JVM

Memory Allocation in JVM

When we create any object, the memory is allocated to object from heap and when the object is collected by Garbage Collector the memory goes back to Heap Memory.

 

Objects Eligible for Garbage Collection in Java

Garbage Collector collects the objects which are not in the use.

One Liner: An object is in use if it can be reached or accessed by the running program.

In JVM, running Java programme have many running threads, each of which is executing the method(s).

Each of the method have reference to objects as arguments or local variable. These reference belong to Root Set of References. Root set of references are the references which are immediately accessible by the programme. Static references variables and references registered through Java Native Interface (JNI) API are also belongs to Root Set of References.

Following are some scenarios in which the object is eligible for garbage collection:

  • The object is not referred by any references or we can say that all the references of the object is set to null.
  • Reference goes out of scope once control exit the block or method.
  • When you set the reference of the object to null, all the object which are referred by only this object is eligible for garbage collection.
  • If there is cyclic reference, then all the object in the cycle are eligible for garbage collection.

E.g.: a=b; b=c; c=a

  • When the object only have weak reference or soft reference.

 

How Garbage Collection in JAVA works

 

To identify which objects are eligible for garbage collection in Java, JVM runs mar-and-sweep-algorithm.

It’s a two-step straightforward process:

  • All object’s references traverse in this algorithm, starting from root set and marked as live object.
  • Then Garbage collector collect those objects which are not marked as live objects and reclaim the heap memory.

 

Garbage Collection in JAVA

Garbage Collection in JAVA

Continue Reading
PageLines