Advertisements
Advertisements
Question
Answer the following in brief. Give a program code example wherever applicable.
What is a parameterised constructor?
Code Writing
Short Answer
Advertisements
Solution
A parameterised constructor is a constructor that accepts one or more parameters to initialise the object with specific values.
class Student
{
int id;
Student(int x) {
id = x;
}
public static void main(String[] args)
{
Student s = new Student(101);
System.out.println(s.id);
}
}
shaalaa.com
Is there an error in this question or solution?
Chapter 4: Constructors - Exercises [Page 115]
