Java OOPs Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Object-Oriented Programming (OOP)?
OOP is a programming paradigm that uses objects to organize code. It involves concepts like encapsulation, inheritance, and polymorphism.
Ques 2. Explain the concept of encapsulation in Java.
Encapsulation is the bundling of data and methods that operate on the data into a single unit known as a class. It helps in hiding the implementation details and restricting access to the internal state of an object.
Example:
class BankAccount {
private double balance;
// other methods and fields
}
Ques 3. What is the difference between abstraction and encapsulation?
Abstraction involves hiding the complex reality while encapsulation involves bundling the data and the methods that operate on the data into a single unit.
Ques 4. How does inheritance work in Java?
Inheritance allows a class to inherit properties and behaviors from another class. It promotes code reusability and establishes a relationship between a superclass and a subclass.
Example:
class Animal {
/* properties and methods */
}
class Dog extends Animal { /* additional methods and properties */ }
Ques 5. Explain polymorphism in Java.
Polymorphism allows objects of different types to be treated as objects of a common type. It can be achieved through method overloading and method overriding.
Example:
void drawShape(Shape s) {
/* method implementation */
}
Ques 6. What is the 'super' keyword in Java?
The 'super' keyword is used to refer to the superclass, invoking its methods, or accessing its fields. It is often used in the context of method overriding.
Example:
super.display();
Ques 7. What is the purpose of the 'final' keyword?
The 'final' keyword is used to make a variable, method, or class constant and cannot be changed. It also prevents a class from being subclassed.
Example:
final int MAX_VALUE = 100;
Ques 8. What is an abstract class in Java?
An abstract class is a class that cannot be instantiated on its own and may contain abstract methods. It is meant to be subclassed by concrete classes.
Example:
abstract class Shape { /* abstract methods */ }
Ques 9. Explain the difference between an interface and an abstract class.
An interface in Java is a collection of abstract methods, while an abstract class can have both abstract and concrete methods. A class can implement multiple interfaces but can extend only one abstract class.
Ques 10. What is the purpose of the 'this' keyword?
The 'this' keyword is used to refer to the current instance of the class. It is often used to differentiate instance variables from local variables when they have the same name.
Example:
public void setName(String name) { this.name = name; }
Ques 11. What is the purpose of the 'static' keyword in Java?
The 'static' keyword is used to create class-level variables and methods. These belong to the class rather than instances of the class.
Example:
static int count = 0;
static void increment() { count++; }
Ques 12. Explain the concept of composition in Java.
Composition involves creating complex objects by combining simpler objects. It allows for better code organization and reusability.
Example:
class Car {
Engine engine;
// other properties and methods
}
Ques 13. What is the purpose of the 'interface' in Java?
An interface in Java is a collection of abstract methods that defines a contract for classes to implement. It supports multiple inheritances and is often used for achieving abstraction.
Example:
interface Printable { void print(); }
Ques 14. What is the purpose of the 'final' method in Java?
A 'final' method in Java cannot be overridden by subclasses. It is used to prevent modification of a method in a subclass.
Example:
class Parent {
final void display() { /* method implementation */ }
}
Ques 15. Explain the concept of method overloading in Java.
Method overloading allows a class to have multiple methods with the same name but different parameters. It improves code readability and flexibility.
Example:
int add(int a, int b) {
/* method implementation */
}
double add(double a, double b) { /* method implementation */ }
Ques 16. What is the 'instanceof' operator used for?
The 'instanceof' operator in Java is used to test if an object is an instance of a particular class or interface. It returns true if the object is an instance; otherwise, it returns false.
Example:
if (obj instanceof MyClass) { /* do something */ }
Ques 17. Explain the concept of method overriding in Java.
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows for runtime polymorphism.
Example:
class Animal { void makeSound() { /* method implementation */ } }
class Dog extends Animal { void makeSound() { /* overridden method implementation */ } }
Ques 18. What is a constructor in Java?
A constructor in Java is a special method used to initialize objects. It has the same name as the class and is called when an object is created.
Example:
class Car { Car() { /* constructor code */ } }
Ques 19. Explain the 'super()' statement in a constructor.
The 'super()' statement is used to invoke the constructor of the superclass. It must be the first statement in the subclass constructor if used.
Example:
class SubClass extends SuperClass { SubClass() { super(); /* other constructor code */ } }
Ques 20. What is the 'this()' constructor call in Java?
The 'this()' constructor call is used to invoke another constructor in the same class. It must be the first statement in the constructor if used.
Example:
class MyClass { MyClass(int x) { this(); /* other constructor code */ }
MyClass() { /* default constructor code */ } }
Ques 21. What is a singleton class in Java?
A singleton class in Java is a class that allows only one instance to be created. It provides a global point of access to that instance.
Example:
class Singleton { private static Singleton instance = new Singleton();
private Singleton() {}
public static Singleton getInstance() { return instance; } }
Ques 22. Explain the concept of abstract method in Java.
An abstract method is a method declared without an implementation in an abstract class. Subclasses must provide the implementation for abstract methods.
Example:
abstract class Shape { abstract void draw(); }
Ques 23. What is the 'throws' clause in Java?
The 'throws' clause in Java is used to declare exceptions that a method might throw. It indicates that the method does not handle the exceptions and passes them to its caller.
Example:
void myMethod() throws IOException { /* method code */ }
Ques 24. Explain the 'try', 'catch', and 'finally' blocks in Java.
The 'try' block contains the code that might throw an exception. The 'catch' block handles the exception, and the 'finally' block contains code that is always executed, regardless of whether an exception is thrown or not.
Example:
try { /* code that might throw an exception */ }
catch (Exception e) { /* handle the exception */ }
finally { /* code that always executes */ }
Ques 25. What is method chaining in Java?
Method chaining involves invoking multiple methods on the same object in a single line. It enhances code readability and is often used in builder patterns.
Example:
StringBuilder sb = new StringBuilder().append("Hello").append(",").append(" World");
Ques 26. Explain the concept of the 'Object' class in Java.
The 'Object' class is the root class for all Java classes. It provides common methods such as 'toString()' and 'equals()' that can be overridden by subclasses.
Example:
class MyClass { /* methods and fields */ }
// implicitly extends Object class
Ques 27. What is the purpose of the 'clone()' method in Java?
The 'clone()' method is used to create a copy of an object. The class of the object must implement the 'Cloneable' interface to use this method.
Example:
class MyClass implements Cloneable { /* methods and fields */
public Object clone() throws CloneNotSupportedException { return super.clone(); } }
Ques 28. Explain the concept of the 'enum' type in Java.
An 'enum' type in Java is a special data type used to define a fixed set of constants. It is often used for representing a group of related values.
Example:
enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Most helpful rated by users: