Skip to main content


Showing posts from October, 2019


Encapsulation (data Hiding) simply means binding object state(fields) and behaviour(methods) together. If you are creating class, you are doing encapsulation. In this guide we will see how to do encapsulation in java program The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.
Simple Example of Encapsulation in Java
How to implement encapsulation in java: 1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class. 2) Have getter and setter methods in the class to set and get the values of the fields.

User Pojo Class

package com.ahold.scanit.datamigration;
//A User class which is a fully encapsulated class.   //It has a private data member and getter and setter methods.   public c…

Java-'this' keyword in java with example

this is a keyword in Java. It can be used inside the method or constructor of a class. It(this) works as a reference to the current object, whose method or constructor is being invoked. This keyword can be used to refer to any member of the current object from within an instance method or a constructor.
Following are the ways to use ‘this’ keyword in java :
1. Using ‘this’ keyword to refer current class instance variables 2. Using this() to invoke current class constructor 3. Using ‘this’ keyword to return the current class instance 4. Using ‘this’ keyword as method parameter 5. Using ‘this’ keyword to invoke current class method 6. Using ‘this’ keyword as an argument in the constructor call
Using ‘this’ keyword to refer current class instance variables
//Java code for using 'this' keyword to  //refer current class instance variables  public class KowledgeFactoryDemo {
int a; int b;
// Parameterized constructor KowledgeFactoryDemo(int a, int b) { this.a = a; this.b = b; }
void display() { // Disp…

Java- 'super' keyword in java with example

The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

The use of super keyword
1) To access the data members of parent class when both parent and child class have member with same name
2) To explicitly call the no-arg and parameterized constructor of parent class
3) To access the method of parent class when child class has overridden that method.

To access the data members of parent class when both parent and child class have member with same name
We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.

class Animal {
String action = "Animal sleeping";

public class Lion extends Animal {

String action = "Lion roaring";

public void action() {


public …

Java -Builder Design Pattern with example

The builder pattern, as name implies, is an alternative way to construct complex objects. This should be used only when you want to build different immutable objects using same object building process. Builder pattern is used to create instance of very complex object having telescoping constructor in easiest way. Method Chaining: In java, Method Chaining is used to invoke multiple methods on the same object which occurs as a single statement. Method-chaining is implemented by a series of methods that return the this reference for a class instance. Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes. Advantages It provides clear separation between the construction and representation of an object.It provides better control over construction process.It supports to change the internal representation of objects.Implementation Constructors in Java are used to create object and can take paramete…

Java: Factory design pattern with example

Factory method is a creational design pattern, i.e., related to object creation. In Factory pattern, we create object without exposing the creation logic to client and the client use the same common interface to create new type of object. The idea is to use a static member-function (static factory method) which creates & returns instances, hiding the details of class modules from user.
A factory pattern is one of the core design principles to create an object, allowing clients to create objects of a library(explained below) in a way such that it doesn’t have tight coupling with the class hierarchy of the library.
Factory Design Pattern Advantages Factory design pattern provides approach to code for interface rather than implementation.Factory pattern removes the instantiation of actual implementation classes from client code. Factory pattern makes our code more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is …