Posts

Showing posts from October, 2019

Java-Encapsulation

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 a

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 the ‘ this ’ keyword in java : 1. Using ‘ this ’ keyword to refer to current class instance variables 2. Using this () to invoke the current class constructor 3. Using ‘ this ’ keyword to return the current class instance 4. Using ‘ this ’ keyword as a method parameter 5. Using ‘ this ’ keyword to invoke the current class method 6. Using ‘ this ’ keyword as an argument in the constructor call Using ‘this’ keyword to refer to current class instance variables //Java code for using 'this' keyword to //refer current class instance variables public class KowledgeFactoryDemo { int a; int b; // Parameterized

Java- 'super' keyword in java with example

The super keyword in Java is a reference variable which is used to refer to immediate parent class object. Whenever you create the instance of a subclass, an instance of the parent class is created implicitly which is referred to by a super reference variable. The use of super keyword 1) To access the data members of parent class when both parent and child class have a member with the same name 2) To explicitly call the no-arg and parameterized constructor of the 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 a member with the same name We can use the super keyword to access the data member or field of the parent class. It is used if parent class and child class have the same fields. class Animal { String action = "Animal sleeping" ; } public class Lion extends Animal { String action = "Lion roaring" ; p

Java -Builder Design Pattern with example

The builder pattern, as the name implies, is an alternative way to construct complex objects. This should be used only when you want to build different immutable objects using the same object-building process. The builder pattern is used to create instances of a very complex object having telescoping constructors in the 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 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 a clear separation between the construction and representation of an object. It provides better control over the construction process. It supports changing the internal representation of objects. Implementation Constructors in J

Java: Factory design pattern with example

The factory method is a creational design pattern, i.e., related to object creation. In Factory pattern, we create an object without exposing the creation logic to the client and the client uses the same common interface to create a new type of object. The idea is to use a static member-function (static factory method) that creates & returns instances, hiding the details of class modules from the 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 The Factory design pattern provides an approach to code for interface rather than implementation. Factory pattern removes the instantiation of actual implementation classes from client code. The factory pattern makes our code more robust, less coupled, and easy to extend. For example, we can easily change PC

Java-Stack with example

Image
Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. This diagram shows the hierarchy of Stack class: Method Summary Modifier and Type Method and Description boolean empty() Tests if this stack is empty. Object peek() Looks at the object at the top of this stack without removing it from the stack. Object pop() Removes the object at the top of this stack and returns that object as the value of this function. Object push ( E  item) Pushes an item onto the top of this stack. int search ( Object  o) Returns the 1-based position where an object is on this stack. Example import java.

Java-Linked List with example

Image
The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index. The important points about Java LinkedList are:  It is an ordered set comprising a variable number of data items. No need to specify; grow and shrink during execution. Element position is assigned during run time. Stored randomly Insertion and deletion of element will be Easier, fast and efficient. Compared to array ,Linked list required more memory Hierarchy of LinkedList class Method & Description boolean add(E e): It is used to append the specified element to the end of a list. void add(int index, E element): It is used to insert the specified element at the specified position index in a list. boolean addAll(Collection<? extends E> c): It is used to append all of the elements in the specified collection to the end