Most Common Java Developer Interview Questions series-12(Core Java,OOP)

In this article, I will be posting commonly asked Java backend developer Interview Questions and their answers, They are common across all interviews. Find my book here Guide To Clear JavaDeveloper Interview, which contains more than 250 such questions and there answers.

Ajay Rathod
5 min readMar 10, 2023

Difference between Aggregation vs composition?

Aggregation and composition are two fundamental concepts in object-oriented programming (OOP) that describe the relationships between classes and objects.

Aggregation refers to the relationship between two objects where one object “has-a” another object, but the second object can exist independently of the first object. In other words, the first object owns or contains the second object, but the second object can also exist on its own.

Composition, on the other hand, is a stronger form of aggregation where the second object cannot exist without the first object. In other words, the first object “owns” the second object, and the second object cannot exist outside the context of the first object.

In terms of implementation, aggregation is typically implemented using a reference or pointer to the second object within the first object, while the composition is typically implemented using a member variable or instance variable within the first object that is an instance of the second object.

To summarize, the main difference between aggregation and composition is the strength of the relationship between the two objects. Aggregation is a weaker form of relationship where the second object can exist independently of the first object, while the composition is a stronger form of relationship where the second object is a vital part of the first object and cannot exist without it.

How to restrict object creation in java?

In Java, you can restrict object creation in several ways. Here are a few common techniques:

Declare the constructor as private: By declaring the constructor as private, you prevent the creation of objects of that class from outside the class. However, objects can still be created within the class itself or by its nested classes. For example:

public class MyClass {
private MyClass() {
// private constructor
}

public static MyClass create() {
return new MyClass();
}
}

In this example, the constructor is declared private, but a static factory method create() is provided to create instances of the class.

Declare the class as abstract: An abstract class cannot be instantiated directly. However, it can be subclassed, and the subclass can be instantiated. This technique is useful when you want to provide a base class but want to prevent direct instantiation of the base class itself.

public abstract class MyClass {
// abstract class
}

Use the enum type: An enum type is a special type of class that has a fixed set of values. Enum values are constants, and you cannot create new instances of them. For example:

public enum MyEnum {
VALUE1,
VALUE2,
VALUE3
}

These are a few techniques that can be used to restrict object creation in Java.

How to create an immutable class in java?

An immutable class in Java is a class whose objects cannot be modified once they are created. Here are the steps to create an immutable class in Java:

  1. Declare the class as final: By declaring the class as final, you prevent other classes from extending it and modifying its behavior.
  2. Declare all instance variables as final: By declaring all instance variables as final, you prevent them from being modified once they are initialized.
  3. Do not provide any setter methods: Since the instance variables are final, you cannot change their values using setter methods. Therefore, it is recommended to not provide any setter methods in an immutable class.
  4. Initialize all instance variables in the constructor: Since the instance variables are final, they must be initialized in the constructor. This can be done either by passing the values as arguments to the constructor or by initializing them with constants or other immutable objects.
  5. Do not allow subclasses to override methods: To ensure that the behavior of an immutable class remains consistent, it is recommended to declare all methods as final. This prevents subclasses from overriding them and modifying their behavior.

Here’s an example of an immutable class in Java:

public final class MyImmutableClass {
private final int value1;
private final String value2;

public MyImmutableClass(int value1, String value2) {
this.value1 = value1;
this.value2 = value2;
}

public int getValue1() {
return value1;
}

public String getValue2() {
return value2;
}
}

What are the in-built immutable classes in java?

In Java, there are several built-in classes that are immutable, meaning their state cannot be changed once they are created. Here are some of the commonly used immutable classes in Java:

  1. String: The String class represents a sequence of characters and is one of the most commonly used classes in Java. Once a String object is created, its contents cannot be modified.
  2. Integer, Long, Double, Float, Short, Byte, and Character: These classes represent primitive data types and are also immutable. Once an object of any of these classes is created, its value cannot be changed.
  3. BigDecimal and BigInteger: These classes are used for performing arithmetic operations on large numbers and are also immutable.
  4. LocalDate, LocalTime, LocalDateTime, and Instant: These classes represent dates and times and are also immutable. They are part of the java.time package introduced in Java 8.
  5. java.util.Optional: This class is used to represent a value that may or may not be present, and is also immutable.

Immutable classes have several benefits, such as being thread-safe and easy to reason about. They also make it easier to write high-quality, bug-free code.

How to create an immutable map in java?

In Java, you can create an immutable map using the Collections class. Here are the steps to create an immutable map:

Create a map with the desired key-value pairs:

Map<String, Integer> mutableMap = new HashMap<>();
mutableMap.put("apple", 1);
mutableMap.put("banana", 2);
mutableMap.put("orange", 3);

Use the Collections.unmodifiableMap() method to create an immutable map from the mutable map:

Map<String, Integer> immutableMap = Collections.unmodifiableMap(mutableMap);

Note that any attempt to modify the immutableMap will result in an UnsupportedOperationException. For example, the following code will throw an exception:

immutableMap.put("grape", 4); // This will throw an UnsupportedOperationException

Creating an immutable map can be useful in situations where you want to ensure that the map’s contents cannot be changed by other parts of the program. This can help prevent bugs and make the code easier to reason about.

Looking for Java Developer interview prep guide follow this book,

Guide To Clear Java Developer Interview:

Gumroad Link — Gumroad

Amazon Link — Amazon

Find my Other stories for your prep.

17 Java Developer Interview Question Series-9(Experienced candidates 0–6 years)

13 New Java Developer Interview Question Series-6 (Microservices and Java Coding)

Thanks for reading

  • 👏 Please clap for the story and follow me 👉on Gumroad and Amazon
  • 📰 Read more content on my Medium (21+ stories on Java Developer interview)

Find my books here:

--

--

Ajay Rathod

Software Engineer @Cisco | Java Programmer | AWS Certified | Writer | Find My Books on Java Interview here - https://rathodajay10.gumroad.com/