📘 OOP Exam Revision Pack – 10 Topics with Code
## 1. Can a class be both abstract and final?
No.
* `abstract` = must be inherited.
* `final` = cannot be inherited.
* Contradiction!
**Java Example:**
```java
abstract final class Test { } // ❌ Compilation error
```
## 2. Four Main OOP Concepts
* **Encapsulation** = wrapping data & methods.
* **Inheritance** = reusing code via subclassing.
* **Polymorphism** = overloading (compile-time) & overriding (runtime).
* **Abstraction** = hide implementation, show essentials.
**Java Example:**
```java
class Student {
private String name; // Encapsulation
public String getName() { return name; }
public void setName(String n) { name = n; }
class Animal { void sound(){ System.out.println("Animal"); } }
class Dog extends Animal { void sound(){ System.out.println("Bark"); } }
```
## 3. Method Overloading vs Overriding
* **Overloading** = same name, different parameters (compile-time).
* **Overriding** = subclass redefines parent method (runtime).
**Java Example:**
```java
class Calc {
int add(int a, int b) { return a+b; }
double add(double a, double b) { return a+b; } // Overloading
}
class Animal {
void sound() { System.out.println("Animal sound"); }
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); } // Overriding
```
## 4. Access Modifiers
* `public` = everywhere
* `private` = only inside class
* `protected` = package + subclasses
* *default* (no keyword) = package-private
**Java Example:**
```java
public class Test {
public int a;
private int b;
protected int c;
int d; // default (package-private)
}
```
## 5. Abstract Class vs Interface
* **Abstract class** → can have fields, constructors, abstract + concrete methods.
* **Interface** → only method declarations (until Java 8), no constructors.
**Java Example:**
```java
abstract class Shape {
abstract void draw();
void info(){ System.out.println("Shape info"); }
interface Drawable {
void draw();
```
## 6. Final Keyword
* **Final class** = cannot be inherited.
* **Final method** = cannot be overridden.
* **Final field** = constant (unchangeable).
**Java Example:**
```java
final class Vehicle { } // cannot extend
class Car {
final int speed = 100; // constant
final void display() { System.out.println("Car"); } // cannot override
```
## 7. Static vs Instance Members
* **Static** = belongs to class, shared.
* **Instance** = unique to each object.
**Java Example:**
```java
class Student {
static String school = "YMSU"; // static shared
String name; // instance
```
## 8. Constructors, Overloading, Chaining
* **Constructor** = initializes objects.
* **Overloading** = multiple constructors with diff params.
* **Chaining** = one constructor calls another using `this()`.
**Java Example:**
```java
class Person {
String name; int age;
Person() { this("Unknown", 0); } // constructor chaining
Person(String n, int a) { name=n; age=a; }
```
## 9. Exception Handling
* **Exception** = runtime error object.
* **Types** = Checked & Unchecked.
* **Handled with** = try, catch, finally, throw, throws.
**Java Example:**
```java
try {
int x = 10/0;
} catch(ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("End of program");
```
## 10. Short Code Examples
**(a) Prime Number Check**
```java
public static boolean isPrime(int n) {
if(n <= 1) return false;
for(int i=2; i<=Math.sqrt(n); i++) {
if(n % i == 0) return false;
return true;
```
**(b) Reverse Identical Arrays**
```java
public static boolean isReverseIdentical(int[] a, int[] b) {
if(a.length != b.length) return false;
for(int i=0; i<a.length; i++) {
if(a[i] != b[b.length-1-i]) return false;
return true;
```