Category Archives: Basic Oops

Difference Between String , StringBuilder And StringBuffer With Example

String String is immutable ( once created can not be changed )object . The object created as a String is stored in the Constant String Pool . Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously. String once assigned Read More →

Difference Between Iterator And Enumeration

Difference Between Iterator And Enumeration The most common interview question in Collections is What is the difference between iterator and enumeration. Iterator Iterator is the interface and found in the java.util package. It has three methods *hasNext() *next() *remove() Enumeration Enumeration is also an interface and found in the java.util package. An enumeration is an Read More →

Difference Between HashMap And HashTable

Difference Between HashMap And HashTable 1. Synchronization or Thread Safe : This is the most important difference between two. HashMap is non synchronized and not thread safe. On the other hand, HashTable is thread safe and synchronized. 2. Null keys and null values : Hashmap allows one null key and any number of null values, Read More →

Threads , Lifecycle Explained With Example

threadstates

What is Thread ? Thread is a sequence of code executed independently with other threads of control with in a single executed program. Multi threading enables you to write in a way where multiple activities can proceed concurrently in the same program. Why Threads? If the program is break into different parts , then it Read More →

ArrayList vs. LinkedList

arraylist-vs-linkedlist-complexity

ArrayList vs. LinkedList   ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It’s elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array. LinkedList is implemented as a double linked list. Its performance on add and Read More →

Can we Overload Static methods in Java ?

Overloading: Overloading is also a feature of OOPs languages like Java that is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input parameters. Can we overload static methods? The answer is ‘Yes’. We can have two Read More →

Why can’t constructors be final, static or abstract in Java?

When you set a method as final, it means : “You don’t want any class override it”, but constructor by JLS definition can’t overridden,so it is clean. When you set a method as ‘abstract’,it means:”Method don’t have any body and you want to implement it at another time in a child class”, but the constructor Read More →

Can there be more than one main method in a Java Program

Yes, you can have as many main methods as you like. You can have main methods with different signatures from main(String[]) which is called overloading, and the JVM will ignore those main methods. You can have one public static void main(String[] args) method in each class. Some people use those methods for testing. They can test the operation Read More →

Can We Declare a Class Private in Java

Outer or concrete class can’t be private in java. For example: Output: Compile Time Error It is because, java follows oops concepts. If you make any class as private, it will not be accessible from another class. So there can’t be inheritance, runtime polymorphism, abstraction etc. But, nested class can be private. For instance: Output: Read More →

Why can’t we use a static class instead of singleton?

Intention of a singleton pattern is to ensure that a single instance of a class is instantiated. So why not static class? Reasons are below :- – One of the key advantage of singleton over static class is that it can implement interfaces and extend classes while the static class cannot (it can extend classes, Read More →

Example of Singleton design pattern

In our previous post, we discussed about singleton design pattern. Now, we will see where is its applicability. According to the definition the singleton pattern should be used when there must be exactly one instance of a class, and when it must be accessible to clients from a global access point. Here are some real Read More →

Singleton Pattern – Java

Singleton Pattern

Singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The singleton pattern is one of the simplest design patterns. It involves only one class which is responsible to instantiate itself, to make sure Read More →