Java Interview Questions (OOPS)
Java Interview Questions (OOPS)

Java Interview Questions (OOPS)

Created
Nov 12, 2025 06:22 AM
Tags

1. What is a package in Java? Explain the different advantages of packages

  • Packages are the collection of related classes and interfaces bundled together. Developers can use packages to modularize the code and enhance its reusability easily. Classes can also import the code within packages and reuse it. Some of the advantages of packages are:
    • Easier access control on the code.
    • A proper hierarchical structure makes it easier to locate related classes.
    • It helps in avoiding name clashes.
    • It can have hidden classes that are not visible to outer classes and are used only within the package.
  1. What are the oops
      • Object-oriented programming (OOPs) is a programming style associated with the following concepts:.
        • Abstraction: It is the method of hiding the implementation details from users and allowing them to see only the functionality-related information.
        • Encapsulation: It refers to wrapping the data and code together as a single unit.
        • Inheritance: It is a process where one class inherits or acquires traits and attributes of another class.
        • Polymorphism: It is the ability of a variable, object, or function to take more than one form.
 

2. What are the differences between aggregation and composition in Java:

Aggregation
Composition
The parent class shares a one-way relationship with the child class.
The parent class owns the child class.
The child class has its individual life cycle.
The child class doesn’t have a lifetime.
Denoted by an empty diamond in Unified Modeling Language notation.
Denoted by a filled diamond in the UML notation.
It is a weak relationship like a bike with an indicator.
It is a stronger relationship like a bike with an engine.
 

3. What are the differences between Heap and Stack Memory:

Particulars
Stack Memory
Heap Memory
Memory
It is used by one thread of execution.
It is used by every part of the application.
Memory Size.
It is comparatively smaller.
It is larger in size.
Flexibility
It is less flexible, so you can’t change the allocated memory.
You can change the allocated memory.
Access
Objects in the stack memory can’t be accessed by other threads.
Objects in the heap memory have global accessibility.
Memory Management.
Follows LIFO (Last In, First Out) order to free memory.
Based on dynamic memory allocation.
Visibility
Here, variables are visible to only the owner thread.
It allows visibility to all threads.
Lifetime
Exists until the end of the execution of the thread.
Exists from the start until the end of application execution.
Efficiency
Fast allocation, access, and deallocation.
Slow allocation, access, and deallocation.
Usage
Contains local primitive and reference variables to objects.
When an object is created, it’s always stored in the Heap space.
Order of Allocation.
Continuous
Random
 

5. What are Wrapper classes in java ?

  • In Java, Wrapper classes provide a mechanism to convert the primitive data types into the reference or object types and vice-versa. Each primitive data type has a class assigned to it known as a wrapper class, as it wraps the primitive type into an object of that class. Conversion primitive type into objects is called autoboxing, whereas converting an object into a primitive data type is known as unboxing.
  • One of the main applications of a wrapper class is changing a value in a method. As Java doesn’t support the class by reference, any changes in a method will have no effect on the original value. So, we can change the original value by converting the types into objects. Moreover, a wrapper class lets us perform serialization by converting an object into a stream.
 

6. What is final keyword in java

  • The final keyword in Java is a predefined term used while declaring values to variables. When we declare a value using the final keyword, the value of a variable remains constant throughout the execution of a program. This special keyword is used as a non-access modifier. We can use the final variable in different contexts, as explained below:.
    • Final variable- When we use the final keyword with a variable, its value can’t be changed once it is assigned. If no value is assigned to the final variable, then a value can be assigned only by using the class constructor.
    • Final method- The final method can’t be overridden by the inheriting class.
    • Final class- Once a class is declared final, we can’t extend it by any subclass, but it can extend any other class.
 

7. What is Polymorphism?

  • Polymorphism in Java is the ability of an object to be processed in multiple ways. It defines a feature of being able to assign different meanings or uses to a variable, object, or function in various contexts. It is used when the parent class refers to the child class reference. There are two types of polymorphism:.
    • Compile-time Polymorphism: It is a method overloading with two or more methods having the same name with arguments and return type.
    • Run-time Polymorphism: It is done using interface and inheritance. Run-time polymorphism of the dynamic method dispatch allows users to resolve the overridden method at runtime instead of compile-time.
 

8. What are differences between Iterator and Enumerators in Java?

Iterator
Enumeration
Iterator is an interface available in the java.util package.
Enumeration is an object generating one element at a time.
Uses three methods, namely hasNext(), next(), and remove().
Uses two methods, which are hasMoreElements() and nextElement().
Allows removing elements from the given collection at the time of the iteration with well-defined semantics.
It is used to pass through a collection, generally of unknown size.
 
 

9. What is the importance of reflection in java?

  • Reflection in Java is a runtime API used for defining the inspection capability of a code on another one, either of itself or of the system, and changing it during the run time. It inspects and modifies the behavior of methods, interfaces, and classes.
  • It is a powerful and beneficial tool that allows programmers to analyze interfaces, classes, methods, and fields during runtime without knowledge of what they are called at the compile time. Java reflection is also used to call methods, set field values, and create new objects.
  • To use user-defined external classes, it’s important to create instances of objects with fully-qualified names. Debuggers can use reflection in Java to assess private members of classes.
  • Let’s understand reflection better with the help of an example. There is an object of an unknown type, and a method ‘fooBar()’ is needed to call on the object.
  • Java’s static typing system doesn’t allow invoking the method unless the object type is known. This is achieved by Java reflection, which allows the code to scan the object and determine if there is a method named ‘fooBar().’ If yes, it will call the method as needed.
 
 

10. What are differences between equals() and == in java?

  • In Java, equals() is defined in the object class and is used to check the equality of two objects that are defined by business logic.
  • On the other hand, ==, also known as the equality operator, is a binary operator used to compare primitives and objects. The object class provides public boolean equals(Object o) method. The default implementation uses == to compare two objects.
 

11. What do you mean by Aggregation ?

  • Aggregation in Java represents the relationship between two classes, which can be best described as a ‘Has-A’ and ‘whole/part’ relationship. This is a one-way relationship between two classes and a specialized version of an association relationship. Here, the aggregate class has the instance of the class it owns.
 

12. What is a singleton class in Java? How to implement a singleton class?

  • When a class is able to possess only one object at a time, it is known as a singleton class in Java. The following are the essential steps to implement a singleton class:.
    • The class must have only one object.
    • The object must have global access.
 

13. Explain the Java thread lifecycle.

  • Here is a brief explanation of the Java thread lifecycle:.
  • New: When a thread is newly created and the start() method hasn’t been invoked, the thread is said to be alive and in the new state.
  • Runnable: Once a thread is started or the start() method is invoked before the JVM calls the run() method, the thread is considered to be in the runnable state. Here, the thread executes the task.
  • Running: Once the run() method is invoked and the thread starts execution of the task, the thread is in the running state.
  • Blocked / Waiting: When a thread is unable to run despite being alive, it is in a non-runnable state. Sometimes, a thread wants to enter the synchronized code but has to wait and stay idle because another thread is operating in that block on the same object. So, the thread waits until the other thread is finished, and once the signal to execute is received, it comes to the running state again.
  • Termination: Termination means the thread is not active anymore. So, when the run() method execution is done, the thread enters the termination state, becoming inactive and can’t be revived.
 

14. Why is the main method static in Java?

  • The main method in Java is static by default because it doesn’t require an object to call the static methods. Also, it allows the compiler to call it before or after creating a class object.
  • The compiler begins the program execution in the main() function in every Java program. Hence, it must be called by the compiler. If the main method is allowed to be non-static, the JVM has to instantiate its class to call the method, which will require more memory allocation.
 

15. C and C++ use pointers but why not Java?

  • For beginners, pointers are quite complicated and unsafe to use. As Java focuses on code simplicity, pointers can make it challenging. Using pointers can also lead to potential errors.
  • Moreover, it can compromise security as users can easily access memory through pointers. It can also make garbage collection slow and prone to errors. Therefore, Java has furnished a certain level of abstraction by not using pointers. It uses references instead that can’t be manipulated, unlike pointers.
 
 

16. Explain the JIT compiler.

  • JIT, also known as the Just-in-Time compiler, is a crucial part of the Java Runtime Environment. It is used to enhance the performance of Java-based applications during run time.
  • JIT compiles parts of byte code that have similar functionality, which reduces the compilation time required for the code to run. We can say that the Java compiler is a translator to convert source code into machine-executable code.
  • Here’s how the compiler works:.
    • The javac compiler converts the Java source code into byte code.
    • The JVM loads the .class files at runtime using an interpreter, which is converted to machine-understandable code.
    • JIT is a crucial part of Java Virtual Machine. Once JIT is enabled, the JVM assesses the method calls in the .class files and compiles them to make code more efficient and native. This also optimizes the prioritized method calls.
    • Last, the JVM executes the optimized code rather than interpreting it again. This improves the performance and speed of execution.
 

17. What are constructors in Java? What are the different types of constructors?

  • Java constructors are the block of code used to initialize an object. A constructor must have the same name as the class. Moreover, it has no return type and is called automatically when an object is created.
  • There are two types of constructors:.
    • Parameterized Constructor: Parameterized constructors accept the parameters so users can initialize the instance variables at the time of instantiating the class with the provided values. In short, we can say that constructors that take arguments are known as parameterized constructors.
    • Default Constructor: This type of constructor doesn’t accept any parameters or inputs but instantiates the class variables with their default values. Hence, it doesn’t take arguments created by default when the user defines no other constructor. It is primarily used for object creation.
 

18. When can you use the super keyword?

  • The super keyword in Java is a reference variable used to access hidden fields and overridden methods/attributes of the parent class. It refers to the immediate parent class object. When we create a subclass instance, we also create a parent class instance referenced by the super reference variable.
  • The super keyword is used in the following cases:.
    • To call the constructor of the immediate parent class within the child class.
    • Access data members of the parent class when the class and subclass have the same name.
    • To access methods of the parent class when the child class has overridden it.
 

19. Differentiate between JDK, JRE, and JVM.

  • Following are the main differences between JVM, JDK, and JRE in Java:.
Criteria
JDK
JRE
JVM
Abbreviation
Java Development Kit.
Java Runtime Environment.
Java Virtual Machine.
Meaning
It is an extensive software development kit used for developing Java applications and includes JRE, JavaDoc, compiler, debuggers, etc.
It is a software package with Java class libraries, JVM and other components required to run Java applications.
It is a platform-dependent abstract machine with 3 specifications- a computer program meeting the JVM requirements, a document describing the JVM implementation requirements, and an instance object for executing the Java byte code and providing the runtime environment for execution.
Main Purpose.
Mainly used for development and execution.
Key purpose is creating an environment to execute the code.
It provides specifications for all the implementations to JRE.
Tools
Offers tools, such as a compiler, debuggers, and more for code development.
Offers libraries and classes that JVM needs to run the program.
Does not include any tools but provides the specification related to implementation.
Summary
JDK = JRE + Development tools.
JRE = JVM + Libraries to execute the application.
JVM = Runtime environment to execute Java byte code.