OOPs_Interview_Questions
OOPs_Interview_Questions
2. 2. Why OOPs?
Advantages of OOPs:
- Better Code Reusability: Through inheritance and polymorphism.
- Easier Maintenance: Encapsulation allows easy updates.
- Improved Data Security: Data hiding restricts unauthorized access.
- Better for Large Software: Logical organization with classes and objects.
3. 3. What is a Class?
A class is a blueprint or template for creating objects. It defines data members (variables)
and methods (functions).
4. 4. What is an Object?
- Encapsulation
- Abstraction
- Polymorphism
- Inheritance
6. 6. What is Encapsulation?
Encapsulation is the bundling of data and methods into a single unit (class) and restricting
access using access modifiers like private, protected, and public.
7. 7. What is Abstraction?
Abstraction is hiding unnecessary details and showing only the relevant features to the
user. Achieved using abstract classes and interfaces.
8. 8. What is Polymorphism?
Polymorphism means 'many forms'. It allows methods to perform different tasks based on
the context.
- Compile-time Polymorphism: Method Overloading.
- Runtime Polymorphism: Method Overriding.
9. 9. What is Inheritance?
Inheritance allows a class (child) to use the properties and methods of another class
(parent). It promotes code reusability.
Advantages:
- Code reusability.
- Easier maintenance and updates.
- Better data security.
Disadvantages:
- Requires skilled programmers.
- Not suitable for all problems.
- Larger program size compared to procedural programming.
- Java
- C++
- Python
- C#
- Ruby
15. 15. Types of Polymorphism
- Overloading: Multiple methods with the same name but different parameters.
- Overriding: Redefining a method in a child class.
A class doesn’t occupy memory. Only objects created from the class use memory.
No. Static methods can be called directly using the class name without creating objects.
A constructor initializes objects. It has the same name as the class and no return type.
An abstract class cannot be instantiated and may contain both abstract and non-abstract
methods.
Example:
```java
abstract class Animal {
abstract void sound();
void eat() { System.out.println("Eating..."); }
}
```