### OOPs with Java - Viva Quick Notes
---
## OOPs Viva Questions with Answers (Java-focused)
---
### 1. What are the four pillars of OOP?
**Answer:**
1. **Encapsulation** Hiding internal data using access modifiers.
2. **Abstraction** Showing only essential features.
3. **Inheritance** Reusing code from a parent class.
4. **Polymorphism** Performing a task in different ways.
---
### 2. What is Encapsulation?
**Answer:**
Encapsulation means **binding data and methods** that operate on data into a single unit (class). It
hides the internal state using `private` access modifier and provides access through public methods
(getters/setters).
---
### 3. What is Abstraction?
**Answer:**
Abstraction means **hiding complex implementation details** and showing only the necessary
features.
Achieved using **abstract classes** and **interfaces** in Java.
---
### 4. What is Inheritance?
**Answer:**
Inheritance is a mechanism where a **child class inherits** properties and behaviors from a **parent
class** using the `extends` keyword.
---
### 5. What is Polymorphism?
**Answer:**
Polymorphism means **one name, many forms**.
Types:
- **Compile-time**: Method overloading
- **Run-time**: Method overriding
---
### 6. What is method overloading?
**Answer:**
Multiple methods with the **same name** but **different parameters** in the same class.
```java
void show(int a) {}
void show(String b) {}
```
---
### 7. What is method overriding?
**Answer:**
Redefining a **parent class method** in a **subclass** with the same signature.
```java
class Animal {
void sound() {}
class Dog extends Animal {
void sound() { [Link]("Bark"); }
```
---
### 8. Difference between Abstraction and Encapsulation?
| Feature | Abstraction | Encapsulation |
|-----------------|------------------------------------|----------------------------------|
| Focus | Hiding implementation | Hiding data |
| Implementation | Using abstract classes/interfaces | Using access modifiers |
| Purpose | Show only essential details | Protect object integrity |
---
### 9. What is the difference between class and object?
**Class**: Blueprint or template.
**Object**: Instance of a class.
```java
class Car {
String color;
Car c = new Car(); // object
```
---
### 10. Can Java support multiple inheritance?
**Answer:**
- **Classes**: No (to avoid ambiguity)
- **Interfaces**: Yes, using multiple interfaces
---
#### 1. Difference between Interface and Abstract Class
| Feature | Interface | Abstract Class |
|-----------------------|--------------------------------------|-------------------------------------|
| Methods | All methods are abstract (Java 7) | Can have both abstract & concrete |
| Variables | `public static final` only | Can have any type |
| Inheritance | Supports multiple | Only single inheritance |
| Keyword | `interface` | `abstract` |
---
#### 2. Constructor Overloading
- Multiple constructors in a class with different parameter lists.
```java
class Student {
Student() { }
Student(String name) { }
```
---
#### 3. Use of `this` keyword
- Refers to current object.
- Resolves variable shadowing, calls constructors, or passes current object.
```java
[Link] = name;
```
---
#### 4. Method Overriding
- Same method name and parameters in subclass.
- Achieves runtime polymorphism.
```java
class Animal {
void sound() { [Link]("Animal Sound"); }
class Dog extends Animal {
void sound() { [Link]("Bark"); }
```
---
#### 5. `==` vs `.equals()`
| `==` | `.equals()` |
|----------------------------------|----------------------------------|
| Compares references | Compares content (for strings) |
| Checks if objects are same | Checks if values are equal |
```java
String a = new String("Hello");
String b = new String("Hello");
[Link](a == b); // false
[Link]([Link](b)); // true
```
---
#### 6. Static Keyword
- Belongs to class, not object.
```java
static int count = 0;
static void display() { }
```
---
#### 7. Package in Java
- Namespace to organize classes and interfaces.
```java
package mypackage;
```
---
#### 8. Access Modifiers
| Modifier | Same Class | Same Package | Subclass | Other |
|------------|------------|--------------|----------|-------|
| private | | | | |
| default | | | | |
| protected | | | | |
| public | | | | |
---
#### 9. JVM, JDK, JRE
| Term | Full Form | Purpose |
|------|---------------------------|----------------------------|
| JVM | Java Virtual Machine | Runs Java bytecode |
| JRE | Java Runtime Environment | JVM + libraries |
| JDK | Java Development Kit | JRE + development tools |
---
#### 10. Exception Handling
```java
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Error: " + [Link]());
} finally {
[Link]("Always runs");
```
---
Good luck for your viva!