English

Answer the following in brief. Give a program code example wherever applicable. What is a parameterised constructor?

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]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 4 Constructors
Exercises | Q 3. (iii) | Page 115
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×