Object-Oriented Programming (OOP) in Java - Interview Preparation
1. What is OOP (Object-Oriented Programming)?
Definition:
OOP is a programming paradigm based on the concept of 'objects', which can contain data (fields) and methods
(functions).
Main goal: Code reusability, modularity, abstraction, and encapsulation.
2. Basic Concepts of OOP
Class: A blueprint for creating objects.
Object: An instance of a class.
3. Four Pillars of OOP
Encapsulation: Wrapping data and methods into a single unit (class).
Inheritance: One class inherits the fields and methods of another.
Polymorphism: One interface, many implementations (overloading/overriding).
Abstraction: Hiding internal details and showing only functionality.
4. Constructor in Java
Types:
- Default Constructor
- Parameterized Constructor
- Copy Constructor (manually implemented in Java).
5. Interface in Java
Used to achieve full abstraction and multiple inheritance.
6. Abstract Class vs Interface
Differences in method body, inheritance, access modifiers, variables, etc.
7. Exception Handling
Handling run-time errors using try, catch, throw, throws, finally.
Types: Checked and Unchecked exceptions.
8. Keywords in Java (OOP Related)
final, finally, this, super, static, new, instanceof.
9. Static Keyword
Used for static variables and static methods.
Object-Oriented Programming (OOP) in Java - Interview Preparation
10. 'this' vs 'super' Keyword
'this' refers to current class object, 'super' refers to parent class.
11. Overloading vs Overriding
Overloading: Same method name, different parameters (compile-time).
Overriding: Same method, different class (run-time).
12. Access Modifiers
public, protected, default, private - define visibility scope.
13. Object Class Methods
Common methods: toString(), equals(), hashCode(), getClass(), clone(), finalize().
14. Wrapper Classes
Convert primitive types to objects: Autoboxing/Unboxing.
15. Inner Classes
Class inside another class.
16. Composition vs Aggregation
Aggregation: Weaker 'has-a' relationship.
Composition: Stronger 'has-a' relationship.
17. Interview Questions from OOPs
1. Difference between abstraction and encapsulation?
2. Can a class implement multiple interfaces?
3. Why no multiple inheritance with classes in Java?
4. Difference between final, finally, and finalize?
5. How does Java achieve runtime polymorphism?
6. Can we override static methods?
7. Default value of object reference?
8. What is constructor chaining?
9. Can interface have private methods?
10. Why use interface if we have abstract class?