Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
6.8k views
in Computer by (26.0k points)

Explain parameterized constructor with syntax and example.

1 Answer

+1 vote
by (25.0k points)
selected by
 
Best answer

The constructors that can take arguments are called parameterized constructors. When a constructor is parameterized, we must pass arguments to the constructor.
When a constructor is parameterized, the object declaration without parameter may not work. We must pass the initial values as arguments to the constructor. 

This can be done in two ways:

1. By implicit call - The implicit call is implemented as follows:
student mk (1200,19); /””implicit call*/

This method is also called the shorthand method, and is used very often as it is shorter, looks better and easy to implement. In the above example, student is a class name and mk is name of object and passed arguments are 1200, 19. One can notice that function name is not taken to invoke constructor.

2. By Explicit Call - The following statement illustrates the explicit call for the parameterized constructor
Student dushyanth = Student (1201, 20); /””explicit call*/

In the above example, Student is class name and dushyanth is object name.
After the = symbol, the name Student refers to parameterized constructor with argument
1201,20.

The parameterized constructor syntax:

Related questions

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...