Java Interview Questions – Object Oriented Programming
Q1. (Asked in Infosys): What are the four main pillars of Object-Oriented Programming in Java?
The four pillars of OOP in Java are:
- Encapsulation: Binding data and methods that work on the data into a single unit (class).
- Inheritance: Allows one class to inherit properties and behavior from another class.
- Polymorphism: Ability to use one method or object in different forms.
- Abstraction: Hiding internal details and showing only essential features.
Q2. (Asked in Wipro): What is the difference between a class and an object in Java?
- A class is a blueprint or template that defines the structure and behavior (data and methods).
- An object is a real instance of a class that occupies memory.
Think of a class as a car design, and an object as a car built from that design.
Q3. (Asked in TCS): Can you explain encapsulation with a real-world example?
Encapsulation is like a medicine capsule โ it wraps medicine (data) inside a shell (class).
In Java, we use private variables and public getters/setters to protect the data from outside access.
Q4. (Asked in Accenture): What is inheritance in Java? Give an example.
Inheritance allows one class (child) to inherit properties and methods of another class (parent).
Example:
class Animal {
void eat() { System.out.println("eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("barking"); }
}
Here, Dog inherits the eat() method from Animal.
Q5. (Asked in MAANG-level mock interview – Google): What is polymorphism and how is it achieved in Java?
Polymorphism means one name, many forms. In Java, it’s achieved by:
- Method Overloading: Same method name with different parameters.
- Method Overriding: Subclass provides specific implementation of a superclass method.
Q6. (Asked in Capgemini): What is method overloading? Can you give an example?
Method overloading is when multiple methods in the same class have the same name but different parameter lists.
Example:
void show() { }
void show(String name) { }
void show(int age, String name) { }
Q7. (Asked in HCL): What is method overriding? When do we use it?
Method overriding is when a subclass provides a specific version of a method already defined in the parent class.
Used when we want different behavior in a subclass.
We use the @Override annotation to indicate it.
Q8. (Asked in Amazon SDE Intern Interview): What is abstraction? How do we achieve it in Java?
Abstraction means hiding complex implementation and showing only the essential part to the user.
In Java, it is achieved using:
- Abstract classes
- Interfaces
Q9. (Asked in Zoho): What is the difference between abstract class and interface in Java?
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Can have both abstract and concrete methods | Only abstract methods (Java 7), Java 8+ allows default/static methods |
| Multiple Inheritance | Not supported | Supported |
| Constructor | Can have constructors | Cannot have constructors |
Q10. (Asked in Byjuโs): Why is Java considered an Object-Oriented Programming language?
Java follows OOP principles like encapsulation, inheritance, polymorphism, and abstraction.
Everything in Java (except primitive types) is treated as an object, making it a strongly object-oriented language.
