0% revised

OOPS

Object-Oriented Programming concepts

0 / 12 revised
4 Pillars of OOP ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
A PIE โ€” Abstraction, Polymorphism, Inheritance, Encapsulation
PillarDefinitionExample
AbstractionHide complexity, show only essentialsAbstract class Shape with abstract area()
EncapsulationBundle data + methods; restrict access via access modifiersPrivate fields with public getters/setters
InheritanceChild class acquires properties/methods of parentDog extends Animal
PolymorphismSame interface, different behavioranimal.speak() โ†’ Dog barks, Cat meows
Class vs Object, Constructors ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
Class = blueprint, Object = instance; constructor initializes
Class: Template defining attributes + methods. Object: Runtime instance of a class.
Constructor: Special method called on object creation. Initializes state.
Destructor: Called when object is destroyed (Python: __del__, Java: finalize โ€” rarely used, GC handles it).
Python: def __init__(self, name): self.name = name
Java: public MyClass(String name) { this.name = name; }
Inheritance Types & Diamond Problem ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
Single, Multi-level, Hierarchical; Diamond = ambiguity in multiple inheritance
TypeDescriptionSupport
SingleA โ†’ BJava, Python
Multi-levelA โ†’ B โ†’ CJava, Python
HierarchicalA โ†’ B, A โ†’ CJava, Python
MultipleA,B โ†’ CPython (MRO), NOT Java (use interfaces)
HybridMix of abovePython only
Diamond Problem: D inherits B and C, both inherit A. Which A's method does D use?
Resolution: Python uses MRO (C3 linearization). Java avoids it โ€” no multiple class inheritance, use interfaces with default methods.
Compile-time vs Runtime Polymorphism ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
Overloading = compile-time (same name, diff params); Overriding = runtime (child redefines parent method)
Overloading (Compile-time)Overriding (Runtime)
BindingEarly/static bindingLate/dynamic binding
MethodSame name, different parametersSame signature in child class
InheritanceNot requiredRequired
Exampleadd(int,int) vs add(float,float)Child.speak() overrides Parent.speak()
Note: Python doesn't have traditional overloading (use default args or *args). Java uses @Override annotation.
Abstract Class vs Interface ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
Abstract = partial implementation + state; Interface = pure contract (pre-Java 8)
Abstract ClassInterface
MethodsAbstract + concreteAll abstract (Java 8+: default methods)
VariablesAny typepublic static final only
InheritanceSingle onlyMultiple allowed
ConstructorYesNo
When to useIS-A with shared codeCAN-DO capability contract
Python: Uses ABC module. No interfaces โ€” use abstract classes or duck typing.
Access Modifiers
OOPS
โ–พ
Quick Recall
public > protected > default > private (Java); Python uses _ convention
Java:
ModifierClassPackageSubclassWorld
publicโœ“โœ“โœ“โœ“
protectedโœ“โœ“โœ“โœ—
defaultโœ“โœ“โœ—โœ—
privateโœ“โœ—โœ—โœ—
Python convention: _protected, __private (name mangling), no true enforcement.
Static vs Instance Members
OOPS
โ–พ
Quick Recall
Static = shared across all objects (class-level); Instance = per-object
Static: Belongs to class, not instance. Shared memory. Access via ClassName.method().
Instance: Each object has its own copy.
Use static for: Utility methods, constants, counters, factory methods.
Java: static int count = 0;
Python: Class variable defined outside __init__, or @staticmethod / @classmethod.
this/self & super
OOPS
โ–พ
Quick Recall
this = current object ref; super = parent class ref
this (Java) / self (Python): Reference to current instance. Resolves ambiguity between field and parameter names. Required in Python method signatures.
super: Calls parent class constructor or methods.
Java: super(args) must be first line in constructor.
Python: super().__init__(args)
Association, Aggregation, Composition ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
Association = uses; Aggregation = has-a (weak); Composition = owns (strong, lifecycle dependent)
RelationshipStrengthLifecycleExample
AssociationWeakIndependentTeacher โ€” Student
AggregationMedium (HAS-A)Independent (part survives)Department โ€” Professor
CompositionStrong (OWNS)Dependent (part dies with whole)House โ€” Room
Interview tip: "If the container is destroyed, does the contained object still exist?" Yes โ†’ Aggregation. No โ†’ Composition.
SOLID Principles ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
SRP, OCP, LSP, ISP, DIP โ€” each principle = one sentence + one example
PrincipleRuleExample
Single ResponsibilityA class has one reason to changeSeparate UserAuth from UserProfile
Open/ClosedOpen for extension, closed for modificationAdd new shapes via subclass, don't modify Shape
Liskov SubstitutionSubtypes must be substitutable for base typesSquare shouldn't break Rectangle's setWidth contract
Interface SegregationMany specific interfaces > one fat interfacePrintable, Scannable vs one MachineInterface
Dependency InversionDepend on abstractions, not concretionsService depends on Repository interface, not MySQLRepo
Design Patterns (Interview Subset) ๐ŸŽฏ
OOPS
โ–พ
Quick Recall
Singleton = one instance; Factory = delegate creation; Observer = pub-sub; Strategy = swap algorithms
Singleton: One instance globally. Private constructor + static getInstance().
class Singleton: _inst=None; @classmethod; def get(cls): cls._inst=cls._inst or cls(); return cls._inst

Factory: Create objects without specifying exact class. ShapeFactory.create("circle") returns Circle().

Observer: Subject notifies observers on state change. Used in event systems, React state, MVC.

Strategy: Encapsulate algorithms, swap at runtime. E.g., SortStrategy with BubbleSort, MergeSort implementations โ€” caller doesn't care which.
OOP vs Procedural
OOPS
โ–พ
Quick Recall
OOP = objects + encapsulation + reuse; Procedural = functions + sequential
OOPProcedural
UnitObject (data + behavior)Function
DataEncapsulated, protectedShared, exposed
ReuseInheritance, compositionFunction calls
ExtensibilityEasy (add new classes)Harder (modify functions)
ExamplesJava, Python, C++C, Pascal
OOP advantages: Modularity, reusability, maintainability, models real-world entities.