Prime Coding
OOPS CHEATSHEET
Key Concepts
1. Class
Definition: A blueprint for creating objects that contain methods and
attributes.
Python:
class Animal:
PR
def __init__(self, name, species):
self.name = name
self.species = species
IM
Java:
E
public class Animal {
C
private String name;
O
private String species;
D
public Animal(String name, String species) {
IN
this.name = name;
this.species = species;
G
}
}
C++:
class Animal {
private:
std::string name;
std::string species;
public:
Animal(std::string name, std::string species) {
OOPS CHEATSHEET
Prime Coding 1
Prime Coding
this->name = name;
this->species = species;
}
};
2. Object
Definition: An instance of a class.
Python:
dog = Animal("Buddy", "Dog")
PR
Java:
Animal dog = new Animal("Buddy", "Dog");
IM
C++:
E
Animal dog("Buddy", "Dog");
C
O
3. Encapsulation
D
Definition: Bundling data and methods that operate on the data within one
IN
unit (class), using access specifiers.
Python:
G
class Example:
def __init__(self):
self.__private_var = 42 # private
self._protected_var = 84 # protected
self.public_var = 168 # public
Java:
public class Example {
private int privateVar = 42;
OOPS CHEATSHEET
Prime Coding 2
Prime Coding
protected int protectedVar = 84;
public int publicVar = 168;
}
C++:
class Example {
private:
int privateVar = 42;
protected:
int protectedVar = 84;
public:
PR
int publicVar = 168;
};
IM
4. Abstraction
E
Definition: Hiding the complex implementation details and showing only
the necessary features.
C
Python:
O
from abc import ABC, abstractmethod
D
IN
class AbstractClass(ABC):
@abstractmethod
G
def abstract_method(self):
pass
class ConcreteClass(AbstractClass):
def abstract_method(self):
print("Implemented abstract method")
Java:
abstract class AbstractClass {
abstract void abstractMethod();
OOPS CHEATSHEET
Prime Coding 3
Prime Coding
class ConcreteClass extends AbstractClass {
void abstractMethod() {
System.out.println("Implemented abstract metho
d");
}
}
C++:
class AbstractClass {
PR
public:
virtual void abstractMethod() = 0; // Pure virt
IM
ual function
};
E
class ConcreteClass : public AbstractClass {
C
public:
void abstractMethod() override {
O
std::cout << "Implemented abstract method"
D
<< std::endl;
}
IN
};
G
5. Inheritance
Definition: Forming new classes using classes that have already been
defined, supporting reusability.
Types:
Single Inheritance: One class inherits from one base class.
Multiple Inheritance: One class inherits from more than one base
class (not supported in Java).
Multilevel Inheritance: A class is derived from another derived class.
OOPS CHEATSHEET
Prime Coding 4
Prime Coding
Hierarchical Inheritance: Multiple classes inherit from one base class.
Hybrid Inheritance: A combination of two or more types of
inheritance.
Python:
class Parent:
def method1(self):
print("Parent method")
class Child(Parent):
def method2(self):
PR
print("Child method")
obj = Child()
IM
obj.method1()
obj.method2()
E
Java:
C
O
class Parent {
void method1() {
D
System.out.println("Parent method");
IN
}
}
G
class Child extends Parent {
void method2() {
System.out.println("Child method");
}
}
Child obj = new Child();
obj.method1();
obj.method2();
OOPS CHEATSHEET
Prime Coding 5
Prime Coding
C++:
class Parent {
public:
void method1() {
std::cout << "Parent method" << std::endl;
}
};
class Child : public Parent {
public:
void method2() {
PR
std::cout << "Child method" << std::endl;
}
IM
};
Child obj;
E
obj.method1();
obj.method2();
C
O
6. Polymorphism
D
Definition: Presenting the same interface for different data types,
IN
achieved through method overriding and method overloading.
Types:
G
Compile-time Polymorphism: Achieved through method overloading
and operator overloading.
Runtime Polymorphism: Achieved through method overriding.
Python:
class Bird:
def sound(self):
print("Chirp")
OOPS CHEATSHEET
Prime Coding 6
Prime Coding
class Dog:
def sound(self):
print("Bark")
def make_sound(animal):
animal.sound()
make_sound(Bird())
make_sound(Dog())
Java:
PR
class Bird {
void sound() {
IM
System.out.println("Chirp");
}
}
E
C
class Dog {
void sound() {
O
System.out.println("Bark");
D
}
}
IN
void makeSound(Animal animal) {
G
animal.sound();
}
makeSound(new Bird());
makeSound(new Dog());
C++:
class Animal {
public:
OOPS CHEATSHEET
Prime Coding 7
Prime Coding
virtual void sound() = 0; // Pure virtual funct
ion
};
class Bird : public Animal {
public:
void sound() override {
std::cout << "Chirp" << std::endl;
}
};
class Dog : public Animal {
PR
public:
void sound() override {
IM
std::cout << "Bark" << std::endl;
}
};
E
void makeSound(Animal* animal) {
C
animal->sound();
O
}
D
makeSound(new Bird());
IN
makeSound(new Dog());
G
Principles of OOP
1. DRY (Don't Repeat Yourself)
Definition: Avoiding duplication by abstracting out commonalities.
Applies to all OOP languages.
2. KISS (Keep It Simple, Stupid)
Definition: Simplicity should be a key goal in design.
Applies to all OOP languages.
OOPS CHEATSHEET
Prime Coding 8
Prime Coding
3. SOLID Principles
Single Responsibility Principle: A class should have only one job.
Open/Closed Principle: Classes should be open for extension but closed
for modification.
Liskov Substitution Principle: Objects of a superclass should be
replaceable with objects of a subclass.
Interface Segregation Principle: Many client-specific interfaces are better
than one general-purpose interface.
Dependency Inversion Principle: Depend on abstractions, not
concretions.
PR
Common OOP Terms
IM
1. Constructor
Definition: A special method called when an object is instantiated.
E
Python:
C
class Example:
O
def __init__(self, value):
D
self.value = value
IN
Java:
G
public class Example {
private int value;
public Example(int value) {
this.value = value;
}
}
C++:
OOPS CHEATSHEET
Prime Coding 9
Prime Coding
class Example {
private:
int value;
public:
Example(int value) {
this->value = value;
}
};
2. Destructor
Definition: A special method called when an object is destroyed.
PR
Python:
IM
class Example:
def __del__(self):
E
print("Destructor called")
C
Java:
O
// Java does not have destructors, but it has a finaliz
D
e method
IN
public class Example {
protected void finalize() {
G
System.out.println("Destructor called");
}
}
C++:
class Example {
public:
~Example() {
std::cout << "Destructor called" << std::en
dl;
OOPS CHEATSHEET
Prime Coding 10
Prime Coding
}
};
3. Method Overloading
Definition: Multiple methods with the same name but different parameters.
Python: Not natively supported but can be simulated using default
arguments.
class Example:
def add(self, a, b, c=0):
return a + b + c
PR
Java:
IM
public class Example {
public int add(int a, int b) {
E
return a + b;
}
C
O
public int add(int a, int b, int c) {
return a + b + c;
D
}
IN
}
G
C++:
class Example {
public:
int add(int a, int b) {
return a + b
int add(int a, int b, int c) {
return a + b + c;
}
};
OOPS CHEATSHEET
Prime Coding 11
Prime Coding
;
}
1. Method Overriding
Definition: Redefining a method in a subclass.
Python:
class Parent:
def show(self):
PR
print("Parent method")
class Child(Parent):
IM
def show(self):
print("Child method")
E
Java:
C
class Parent {
O
void show() {
D
System.out.println("Parent method");
}
IN
}
G
class Child extends Parent {
@Override
void show() {
System.out.println("Child method");
}
}
C++:
OOPS CHEATSHEET
Prime Coding 12
Prime Coding
class Parent {
public:
virtual void show() {
std::cout << "Parent method" << std::endl;
}
};
class Child : public Parent {
public:
void show() override {
std::cout << "Child method" << std::endl;
PR
}
};
IM
2. Super
Definition: A function or keyword used to call a method from the parent
E
class.
C
Python:
O
class Parent:
D
def __init__(self, value):
IN
self.value = value
G
class Child(Parent):
def __init__(self, value, extra_value):
super().__init__(value)
self.extra_value = extra_value
Java:
class Parent {
int value;
Parent(int value) {
OOPS CHEATSHEET
Prime Coding 13
Prime Coding
this.value = value;
}
}
class Child extends Parent {
int extraValue;
Child(int value, int extraValue) {
super(value);
this.extraValue = extraValue;
}
}
PR
C++:
IM
class Parent {
protected:
E
int value;
C
public:
Parent(int value) {
O
this->value = value;
D
}
};
IN
class Child : public Parent {
G
private:
int extraValue;
public:
Child(int value, int extraValue) : Parent(valu
e) {
this->extraValue = extraValue;
}
};
Common OOP Practices
OOPS CHEATSHEET
Prime Coding 14
Prime Coding
1. Composition over Inheritance
Definition: Prefer composition (has-a relationship) to inheritance (is-a
relationship) to achieve better modularity.
Python:
class Engine:
def start(self):
print("Engine started")
class Car:
def __init__(self):
PR
self.engine = Engine()
def start(self):
IM
self.engine.start()
E
Java:
C
class Engine {
O
void start() {
System.out.println("Engine started");
D
}
IN
}
G
class Car {
private Engine engine;
Car() {
this.engine = new Engine();
}
void start() {
engine.start();
OOPS CHEATSHEET
Prime Coding 15
Prime Coding
}
}
C++:
class Engine {
public:
void start() {
std::cout << "Engine started" << std::endl;
}
};
PR
class Car {
private:
IM
Engine engine;
public:
void start() {
E
engine.start();
C
}
};
O
D
2. Design Patterns
IN
Definition: Reusable solutions to common software design problems (e.g.,
Singleton, Factory, Observer).
G
Applies to all OOP languages.
3. UML Diagrams
Definition: Visual representations of the design of a system (e.g., Class
Diagrams, Sequence Diagrams).
Applies to all OOP languages.
OOPS CHEATSHEET
Prime Coding 16