⬅ Previous Topic
Polymorphism in Programming - OOPNext Topic ⮕
Instance vs Class Variables and Methods - OOP⬅ Previous Topic
Polymorphism in Programming - OOPNext Topic ⮕
Instance vs Class Variables and Methods - OOPWhen working with object-oriented programming (OOP), objects are created from classes. But how do these objects get initialized with starting values or resources? That’s where constructors come in.
Similarly, when an object is no longer needed, and its resources should be released, destructors help perform cleanup operations automatically.
A constructor is a special routine that gets called automatically when a new object is created. It is used to initialize the object with default or given values.
Think of it as a “setup function” for your object. Every time you create a new object, the constructor runs without you needing to explicitly call it.
CLASS Car:
FUNCTION Constructor():
PRINT "A car object has been created!"
OBJECT myCar = NEW Car()
Output:
A car object has been created!
Question: Why don’t we call the constructor ourselves when creating an object?
Answer: Constructors are automatically invoked during object creation. This ensures that all objects are properly initialized before use.
Constructors can also accept arguments. This allows different objects to start with different values.
CLASS User:
FUNCTION Constructor(name, age):
SET self.name = name
SET self.age = age
PRINT "User created: " + name + ", Age: " + age
OBJECT user1 = NEW User("Alice", 25)
OBJECT user2 = NEW User("Bob", 30)
Output:
User created: Alice, Age: 25 User created: Bob, Age: 30
Question: Can a class have more than one constructor?
Answer: Some languages support constructor overloading (multiple constructors with different parameters), but generally, you can mimic this behavior using conditional logic inside a single constructor.
A destructor is the counterpart of a constructor. It is called automatically when the object is about to be destroyed or goes out of scope.
It is mainly used for cleanup activities like closing files, releasing memory, or disconnecting from services.
CLASS FileHandler:
FUNCTION Constructor():
PRINT "File opened"
FUNCTION Destructor():
PRINT "File closed"
OBJECT file = NEW FileHandler()
// Do some file operations
DESTROY file // This will trigger the destructor
Output:
File opened File closed
Question: What if we forget to destroy the object manually?
Answer: In most modern programming environments, destructors are triggered automatically by the system when the object is no longer in use (like garbage collection). However, in some low-level contexts, manual destruction is necessary.
Constructors and destructors are essential in object-oriented programming to automate the creation and cleanup of objects. They ensure smooth lifecycle management and help maintain clean, efficient codebases.
⬅ Previous Topic
Polymorphism in Programming - OOPNext Topic ⮕
Instance vs Class Variables and Methods - OOPYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.