Posts

Java - 'super' keyword in java with example

The super keyword in Java is a reference variable that is used to refer to the 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 a super keywords,  To access the data members of parent class when both parent and child class have a member with the same name To explicitly call the no-arg and parameterized constructor of the parent class  To access the method of parent class when the child class has overridden that method. 1. 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" ;

Java - Builder Design Pattern - Example

Builder is a creational pattern that provides an efficient way to build objects. Compared with the traditional way of using multiple constructors, using the Builder pattern can ensure the readability and stability of the code. This mode is particularly useful when creating complex objects. Use Builder pattern in the following situations: When the algorithm for creating a complex object should be independent of the object's components and how they are assembled When the construction process must allow the constructed object to have different representation Advantages 1. It provides a clear disseverment between the construction and representation of an object. 2. It provides better control over the construction process. 3. It fortifies transmuting the internal representation of objects. Implementation   Let's see an example and learn how to implement a builder pattern. Consider a POJO of Employee below. public class Employee { // All final attributes private final S

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

Java -Loops

Image
Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Java we have three types of basic loops for loop do-while loop while loop 1. while loop A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntax: while ( boolean condition) { statements... } Note: The important point to note when using the while loop is that we need to use increment or decrement statement inside the while loop so that the loop variable gets changed on each iteration, and at some point, the condition returns false. This way we can end the execution of the while loop otherwise the loop would execute indefinitely. Simple while loop example public class KnfWhileLoopExample { public static void main( String [] args) { int i = 0 ; while (i <= 10 ) { System.out.println(i); i++;

Java-Data Types-example

Image
Variables reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. There are two types of data types in Java: Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double. Non-primitive data types:  he non-primitive data types include Classes, Interfaces, and Arrays. Primitive data Primitive data are only single values; they have no special capabilities. There are 8 primitive data types boolean: boolean data type represents one bit of information There are only two possible values: true and false This data type is used for simple flags that track true/false conditions Def