- Encapsulation: Bundles data and methods together.
- Abstraction: Hides complex implementation.
- Inheritance: Promotes code reusability.
- Polymorphism: Allows flexibility in method calls.
Java, C++, Python, C#, Swift, Kotlin.
| Feature | Interface | Abstract Class |
|---|---|---|
| Methods | Only abstract methods (Java 7). Default/static methods allowed (Java 8+). | Can have both abstract & concrete methods. |
| Fields | Only constants (public, static, final). | Can have instance variables. |
| Constructor | No constructor. | Can have constructors. |
| Usage | Used for capability-based design (e.g., Runnable). |
Used for common base functionality (e.g., Animal). |
| Inheritance | A class can implement multiple interfaces. | A class can extend only one abstract class. |
equals(): Used to compare object contents, not memory reference.hashCode(): Used for efficient storage in hash-based collections (e.g.,HashMap).- Override When? When using objects as keys in hash-based collections or defining custom equality logic.
- Occurs in multiple inheritance when a class inherits from two classes/interfaces with the same method signature.
- Java prevents multiple class inheritance.
- Use interfaces: Java 8 allows default methods, but if a conflict occurs, the subclass must override it.
- Frees memory by removing unused objects to prevent memory leaks.
- Runs automatically in JVM. Uses algorithms like Mark and Sweep, Generational GC, G1 GC.
- Variable: Shared across all instances (
static int count). - Method: Can be called without an object (
Math.sqrt()). - Block: Runs once when class is loaded (
static {}block). - Nested Class: Can exist without an outer class instance (
static class Helper).
- Object state cannot be changed after creation.
- Thread safety, caching, and reducing unintended side effects.
- Declare fields
private final, remove setters, and return new objects instead of modifying existing ones.
String, Wrapper classes (Integer,Double),java.timeAPI.
| Feature | Composition | Aggregation |
|---|---|---|
| Definition | Strong association (part cannot exist without whole). | Weak association (part can exist independently). |
| Example | Car and Engine (engine is destroyed if car is destroyed). |
Department and Professor (professor can exist without department). |
| Feature | Cohesion | Coupling |
|---|---|---|
| Definition | How strongly related methods & data are within a class. | Degree of dependency between modules/classes. |
| Good Practice | High cohesion is good (single responsibility). | Low coupling is good (independent components). |
| Feature | Heap | Stack |
|---|---|---|
| Usage | Stores objects, dynamically allocated memory. | Stores method calls, local variables, function execution context. |
| Access Speed | Slower | Faster |
| Lifespan | Exists till GC removes it. | Exists until method execution completes. |
Unwanted events that disrupt program execution.
- Checked Exception – Must be handled (
IOException,SQLException). - Unchecked Exception – Runtime errors (
NullPointerException,ArithmeticException). - Error – System-level issue (
OutOfMemoryError,StackOverflowError).
Simple, readable, maintainable, and efficient.
- Meaningful names.
- Small functions.
- Follows SOLID principles.
- No unnecessary complexity.
- If a subclass defines a
staticmethod with the same signature as astaticmethod in the parent class, the subclass method hides the superclass method instead of overriding it.
class Parent {
static void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
static void display() {
System.out.println("Child");
}
}Definition:
Abstraction is the process of hiding implementation details and showing only the essential features of an object.
Key Points:
- Focuses on what an object does rather than how it does it.
- Achieved using abstract classes and interfaces.
- Helps in reducing complexity and increasing reusability.
Example in Java:
abstract class Animal {
abstract void makeSound(); // Abstract method (no implementation)
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}Definition:
Polymorphism allows objects of different classes to be treated as objects of a common super class. It enables a single interface to represent different data types.
Key Points:
- Two types: Compile-time (method overloading) and Run-time (method overriding).
- Improves code flexibility and reusability.
- Supports dynamic method dispatch.
Example in Java:
class Animal {
void makeSound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog(); // Runtime Polymorphism
myAnimal.makeSound(); // Output: Bark
}
}| Feature | Abstraction | Polymorphism |
|---|---|---|
| Definition | Hiding implementation details | Multiple forms of the same method |
| Purpose | Reduce complexity | Increase flexibility |
| Achieved by | Abstract classes & interfaces | Method overloading & overriding |
| Example | Abstract makeSound() method in Animal |
makeSound() behaves differently in Dog |