OOP Notes
Learn how constructors work — default constructors, parameterized constructors, copy constructors, and constructor overloading across Java, Python, and C++.
// Usage Circle c = new Circle(); // radius=1.0, color="black"
public class Circle { private double radius; private String color;
// Parameterized constructor public Circle(double radius, String color) { if (radius <= 0) { throw new IllegalArgumentException("Radius must be positive"); } this.radius = radius; this.color = color; }
public double getArea() { return Math.PI * radius * radius; } }
// Usage Circle c = new Circle(5.0, "red"); // radius=5.0, color="red"
#include <iostream> #include <string>
class Rectangle { private: double width, height; std::string label;
public: // Regular constructor Rectangle(double w, double h, std::string l) : width(w), height(h), label(l) {}
// Copy constructor Rectangle(const Rectangle& other) : width(other.width), height(other.height), label(other.label + "_copy") { std::cout << "Copy constructor called!" << std::endl; }
void display() const { std::cout << label << ": " << width << "x" << height << std::endl; } };
int main() { Rectangle original(10, 5, "Original"); Rectangle copy(original); // Copy constructor called!
original.display(); // Original: 10x5 copy.display(); // Original_copy: 10x5 return 0; }
public class Employee { private String name; private String department; private double salary; private int yearsExperience;
// Constructor 1: Full details public Employee(String name, String department, double salary, int years) { this.name = name; this.department = department; this.salary = salary; this.yearsExperience = years; }
// Constructor 2: New hire (default salary and experience) public Employee(String name, String department) { this(name, department, 50000.0, 0); // Calls Constructor 1 }
// Constructor 3: Just a name (intern) public Employee(String name) { this(name, "Unassigned", 30000.0, 0); }
public void display() { System.out.printf("%s | %s | $%.0f | %d years%n", name, department, salary, yearsExperience); } }
// Usage — three ways to create an Employee Employee veteran = new Employee("Alice", "Engineering", 120000, 10); Employee newHire = new Employee("Bob", "Marketing"); Employee intern = new Employee("Charlie");
class Rectangle: def __init__(self, width=1, height=1, color="black"): """Python constructor with default values""" if width <= 0 or height <= 0: raise ValueError("Dimensions must be positive") self.width = width self.height = height self.color = color self.area = width * height # Computed during construction
def scale(self, factor): self.width *= factor self.height *= factor self.area = self.width * self.height
def __str__(self): return f"Rectangle({self.width}x{self.height}, {self.color})"
r1 = Rectangle() # 1x1, black r2 = Rectangle(5, 3) # 5x3, black r3 = Rectangle(10, 10, "blue") # 10x10, blue r4 = Rectangle(width=7, color="red") # 7x1, red (keyword args)
print(r2) # Rectangle(5x3, black)
public class DatabaseConnection { private String host; private int port; private String database; private String username; private String password; private int timeout;
// Full constructor public DatabaseConnection(String host, int port, String database, String username, String password, int timeout) { this.host = host; this.port = port; this.database = database; this.username = username; this.password = password; this.timeout = timeout; }
// Chain to full constructor with default timeout public DatabaseConnection(String host, int port, String database, String username, String password) { this(host, port, database, username, password, 30); // 30s default }
// Chain with default port and timeout public DatabaseConnection(String host, String database, String username, String password) { this(host, 5432, database, username, password, 30); // PostgreSQL default port } }
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Constructors in OOP.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Object Oriented Programming (OOP) topic.
Search Terms
object-oriented-programming, object oriented programming (oop), object, oriented, programming, oop, basics, constructors
Related Object Oriented Programming (OOP) Topics