Class
-
Definition: A class is a blueprint or template that defines the structure and behavior (data members and methods) of objects.
-
Example:
class Car { int speed; void drive(); }
Object
-
Definition: An object is an instance of a class. It has its own identity, state (values of attributes), and behavior (methods).
-
Example:
Car myCar = new Car();
Constructor & Destructor
-
Constructor: A special method used to initialize objects. Called automatically when an object is created.
-
Destructor: A special method used to clean up resources when an object is destroyed (C++), or garbage collected (Java/C#).
Association, Aggregation, Composition
-
Explanation:
-
Association: Relationship between two classes.
-
Aggregation: "Has-a" relationship, weaker. (Car has an Engine, Engine can exist independently.)
-
Composition: Strong "has-a", lifecycle tied. (Human has a Heart, cannot exist separately.)
-
Interfaces & Abstract Classes
-
Abstract Class: Can have both abstract methods (no body) and concrete methods. Cannot be instantiated.
-
Interface: Only method signatures (in Java < 8). Defines a contract. A class must implement it.
Four pillars of OOPS:-
-
Encapsulation
-
Definition: Encapsulation is the concept of binding data (variables) and behavior (methods) together inside a class, and restricting direct access to some components using access modifiers (
private,public,protected). -
Public: Accessible everywhere.
-
Private: Accessible only within the same class.
-
Protected: Accessible within the same class, subclasses, and same package (Java).
-
Default (Java): Accessible within the same package.
-
Abstraction
-
Definition: Abstraction is the process of hiding implementation details and exposing only the essential features. Achieved using abstract classes and interfaces.
-
Benefit: Reduces complexity and increases flexibility.
-
Inheritance
-
Definition: Inheritance is the mechanism where one class (child/subclass) can inherit properties and methods from another class (parent/superclass).
-
Benefit: Promotes code reuse and hierarchy.
-
Types: Single, Multiple (not in Java, but in C++), Multilevel, Hierarchical, Hybrid.
-
Polymorphism
-
Definition: Polymorphism means "many forms". It allows the same method or operator to behave differently based on context.
-
Types:
-
Compile-time (Overloading) → Same method name, different parameters.
-
Runtime (Overriding) → Subclass provides its own implementation of a met
-
class Test {
void show(int a) {}
void show(double b) {} // Overloading
void display() { System.out.println("Parent"); }
}
class SubTest extends Test {
@Override
void display() { System.out.println("Child"); } // Overriding
}
Overloading vs Overriding
-
Overloading: Same method name but different parameter list (compile-time).
-
Overriding: Same method name and parameters in subclass, with different implementation (runtime).
Virtual Functions (C++)
- A function in the base class that can be overridden in the derived class, enabling runtime polymorphism.
Dynamic Binding vs Static Binding
-
Static Binding (Early): Method call is resolved at compile time (overloading).
-
Dynamic Binding (Late): Method call is resolved at runtime (overriding with virtual methods).