Menu
Question Index
...

Define copy constructor for the Student class. A copy constructor is a constructor which takes another Student object as parameter and then copies every field into the newly created object.



class DefineCopyConstructor
{
    public static void main(String s[])
    {
        Student raja = new Student("Raja", 83, 'B');
        Student rajaCopy = new Student(raja);
        
        System.out.println("The details in the copied object are : " + rajaCopy.name + " - " + rajaCopy.marks + " - " + rajaCopy.section);

    }



}
class Student
{
    String name;
    int marks;
    char section;

    Student(String name, int marks, char section)
    {
        this.name = name;
        this.marks = marks;
        this.section = section;
    }

}

Doubts

Problems

Topic: Unknown

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App