Constructor
Presented By: Shereen Sikander
Constructor
• Constructor is a special type of method whose name is same as class
name.
• The main purpose of a constructor is to initialize an object.
• Every java class has a constructor.
• Constructor is automatically called at the time of object creation.
• Constructor never contains any return type, including void.
Code
Code
Interview Questions
• What is a need of a Constructor?
• Do constructors have any return type?
Types of Constructors
• Default
• Parametrized
• Copy Constructor
• Private Constructor
Default Constructor
• A constructor which doesn’t have any parameter is called a default
constructor.
• Syntax:
Class A
{
A()
{
}
}
Example of Default Constructor
Example of Default Constructor
Default Constructor
• If we don’t write our constructor, then java compiler adds its default
constructor.
Interview Question
• What is a need of a default constructor when java compiler adds a
constructor on its own?
Parametrized Constructor
• A constructor in which we pass one or more than one parameter is called a
parametrized constructor
• Syntax
Class demo {
demo ( int a, String b)
{
}
Example of Parametrized
Constructor
Interview Question
• Can we make more than one parametrized constructor in a single
class? …activity
• Can non-premitive data types be passed in parametrized constructor?
• Can a compiler add a default constructor on its own if only a
parameterized constructor is defined in a class?
Real time scenario to use
Parametrized Constructor
• A real-time example of using a parameterized
constructor would be when creating a "Customer"
object in an online shopping application, where you
would use a parameterized constructor to initialize a
new customer with their name, email address, and
shipping address, passing these details as arguments
when creating the customer object; this ensures each
customer is set up with their specific information from
the start, rather than needing to manually set these
values late
Example via code…
Copy Constructor
• Whenever an object reference is passed in a constructor, then it is
called a copy constructor.
• A copy constructor is a member function that initializes
an object using another object of the same class.
• Syntax:
Class class-name {
class-name (class-name ref)
{ …}
}
Example via code..
Activity
• Code previous slide example with default constructor.
Real time scenario to use Copy
constructor
• Cloning data structures:
In a game development scenario, you might use a copy constructor to
create a duplicate of a character's state before performing an action,
allowing you to revert to the original state if needed.
• Data manipulation in a function:
When passing an object to a function that might modify its data, you can
use a copy constructor to create a copy of the object within the function,
preventing unintended changes to the original object.
• Returning a modified object from a function:
If a function needs to return a modified version of an object, creating a
copy using the copy constructor ensures the original object remains
unchanged.
Private Constructor
• A private constructor is a special type of constructor
that can only be accessed within the class it is defined
in. This prevents other classes from creating instances
of the class.
• Syntax:
Class class-name{
private class-name()
{
}
}
Example via code..
Real time use of Private Constructor
• By marking a constructor as private, developers can
control instantiation within the class, restricting how
and when an object is created.