0% completed
Parameterized Constructor
On This Page
Understanding the Parameterized Constructor
Syntax of a Parameterized Constructor
Example 1: Car Class with Parameterized Constructor
Example 2: Car Class with Different Parameters
Understanding the Parameterized Constructor
A parameterized constructor is a constructor that accepts one or more parameters to initialize an object's member variables with specific values provided during object creation.
- Characteristics:
- Contains parameters.
- Has the same name as the class.
- Does not have a return type.
Syntax of a Parameterized Constructor
class ClassName { // Parameterized constructor ClassName(dataType parameter1, dataType parameter2, ...) { // Initialization code } }
Explanation:
- ClassName(parameters): The constructor method with the same name as the class and one or more parameters.
- dataType parameter: Represents the type and name of each parameter that will be used to initialize member variables.
- Initialization code: Assigns the values of parameters to the class's member variables or performs other setup tasks.
Example 1: Car Class with Parameterized Constructor
In this example, we define a Car class with a parameterized constructor that initializes its member variables with specific values provided during object creation.
Explanation:
-
Car(String brand, String color, int year):
This is the parameterized constructor for theCarclass. It takes three parameters:brand,color, andyear. -
this.brand = brand;,this.color = color;,this.year = year;:
Thethiskeyword refers to the current object. These lines assign the values of the parameters to the class's member variables. -
Car myCar = new Car("Tesla", "White", 2023);:
Creates a newCarobject namedmyCarusing the parameterized constructor, initializing it with the brand "Tesla", color "White", and year 2023. -
myCar.displayDetails();:
Calls thedisplayDetailsmethod to print the initialized values ofmyCar.
Example 2: Car Class with Different Parameters
This example demonstrates how you can create multiple Car objects with different initial values using the same parameterized constructor.
Explanation:
-
Car(String brand, String color, int year):
The same parameterized constructor is used to initialize differentCarobjects with varying values. -
Car car1 = new Car("BMW", "Black", 2020);:
Creates aCarobjectcar1with brand "BMW", color "Black", and year 2020. -
Car car2 = new Car("Audi", "Blue", 2021);:
Creates anotherCarobjectcar2with brand "Audi", color "Blue", and year 2021. -
car1.displayDetails();andcar2.displayDetails();:
Calls thedisplayDetailsmethod for both objects to print their respective initialized values.
A parameterized constructor enables the initialization of an object's member variables with specific values at the time of creation. Unlike the default constructor, which assigns default or predefined values, parameterized constructors provide the flexibility to set unique initial states for each object.
In this lesson, we explored the syntax and usage of parameterized constructors using the Car class example. We saw how to create objects with different initial values and how the this keyword helps in distinguishing between class members and constructor parameters.
Reading Progress
0%
On This Page
Understanding the Parameterized Constructor
Syntax of a Parameterized Constructor
Example 1: Car Class with Parameterized Constructor
Example 2: Car Class with Different Parameters