![]()
Object-Oriented Programming (OOP) is a fundamental concept in Java, and interviewers often test candidates on their understanding of OOP principles. Below are some common OOP concept interview questions along with explanations and examples.
1. What are the four principles of OOP?
The four principles of OOP are:
- Encapsulation: Bundling data and methods that operate on the data into a single unit (class) and restricting access to some of the object’s components.
- Inheritance: Creating a new class (subclass) from an existing class (superclass) to reuse code and establish a relationship between classes.
- Polymorphism: Allowing objects of different classes to be treated as objects of a common superclass, enabling method overriding and overloading.
- Abstraction: Hiding complex implementation details and exposing only essential features of an object.
2. What is Encapsulation?
Encapsulation is the mechanism of wrapping data (variables) and code (methods) together as a single unit. It also restricts direct access to some of an object’s components, which is achieved using access modifiers like private, protected, and public.
Example:
class Employee {
private String name; // Private variable
private int age;
// Public methods to access private variables
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) { // Validation
this.age = age;
}
}
}
3. What is Inheritance?
Inheritance allows a class (subclass) to inherit fields and methods from another class (superclass). It promotes code reuse and establishes an “is-a” relationship.
Example:
class Animal { // Superclass
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal { // Subclass
void bark() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Subclass method
}
}
4. What is Polymorphism?
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
Example of Method Overriding:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog(); // Upcasting
myAnimal.sound(); // Calls Dog's sound() method
}
}
Example of Method Overloading:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Calls int add()
System.out.println(calc.add(5.5, 10.5)); // Calls double add()
}
}
5. What is Abstraction?
Abstraction is the process of hiding complex implementation details and exposing only essential features. It is achieved using abstract classes and interfaces.
Example of Abstract Class:
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
Example of Interface:
interface Drawable {
void draw(); // Abstract method
}
class Circle implements Drawable {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
Drawable drawable = new Circle();
drawable.draw();
}
}
6. What is the difference between an Abstract Class and an Interface?
| Abstract Class | Interface |
|---|---|
| Can have abstract and non-abstract methods. | Can only have abstract methods (before Java 8). |
| Can have instance variables. | Can only have constants (public static final). |
| Supports single inheritance. | Supports multiple inheritance. |
| Can have constructors. | Cannot have constructors. |
| Used for partial abstraction. | Used for full abstraction. |
7. What is the super keyword?
The super keyword is used to refer to the immediate parent class object. It is used to:
- Call the parent class constructor.
- Access parent class methods and variables.
Example:
class Animal {
String sound = "Animal sound";
Animal() {
System.out.println("Animal constructor");
}
void makeSound() {
System.out.println(sound);
}
}
class Dog extends Animal {
Dog() {
super(); // Calls Animal constructor
System.out.println("Dog constructor");
}
void makeSound() {
super.makeSound(); // Calls Animal's makeSound()
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
}
}
8. What is the this keyword?
The this keyword refers to the current instance of the class. It is used to:
- Differentiate between instance variables and parameters.
- Call one constructor from another (constructor chaining).
Example:
class Employee {
private String name;
Employee(String name) {
this.name = name; // Differentiates instance variable from parameter
}
void display() {
System.out.println("Name: " + this.name);
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("John");
emp.display();
}
}
9. What is Method Overriding?
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters as the method in the superclass.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound(); // Calls Dog's sound() method
}
}
10. What is Method Overloading?
Method overloading occurs when a class has multiple methods with the same name but different parameters (number, type, or order). It is resolved at compile time.
Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Calls int add()
System.out.println(calc.add(5.5, 10.5)); // Calls double add()
}
}
11. What is the difference between == and .equals()?
==compares object references (memory addresses)..equals()compares the content of objects (values).
Example:
public class Main {
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String("Hello");
System.out.println(str1 == str2); // false (different references)
System.out.println(str1.equals(str2)); // true (same content)
}
}
12. What is a Singleton Class?
A singleton class ensures that only one instance of the class is created and provides a global point of access to it.
Example:
class Singleton {
private static Singleton instance;
private Singleton() {} // Private constructor
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
Singleton obj1 = Singleton.getInstance();
Singleton obj2 = Singleton.getInstance();
System.out.println(obj1 == obj2); // true (same instance)
}
}
13. What is Composition?
Composition is a design principle where a class contains an object of another class. It represents a “has-a” relationship.
Example:
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
private Engine engine; // Composition
Car() {
engine = new Engine();
}
void start() {
engine.start();
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
14. What is Association, Aggregation, and Composition?
- Association: A relationship between two classes (e.g., a teacher and students).
- Aggregation: A weak “has-a” relationship where the child can exist independently of the parent (e.g., a department and professors).
- Composition: A strong “has-a” relationship where the child cannot exist without the parent (e.g., a car and its engine).
By understanding these OOP concepts and practicing examples, you can confidently answer Java OOP interview questions.
