Constructors
ICSE 10th
Constructor
• It is a member method with a name same as that of the class name
used to initialize the instance varibles of the objects
Invoking a Constructor
• A construction is invoked at the time of creating an object of that
class
• Syntax
• <class name><object name>=new <constructor>
Features
• The Constructor has same name as class name
• It automatically gets invoked while creating an object
• It has no return type – NO arthimetical or logical operation can be
performed on the constructor so it not required
• The constructor is always public
• The constructor is overloaded automatically
• Is only to intitialize the data members/instance variables
Types of Constructors
• Default
• Parameterized
• Default : Initializes the instance variables of an object with the
definite values readily available
Program 1
• Class name : Prime
• Data members : int n
• Member methods :
• Prime() : default constructor to initiaze n
• Void input(int x):to assign n with x
• Void display () to check whter the number is prime or not
Program 2
• Class name : Marks
• Data members : name,age,m1,m2,m3,maximum,average
• Member methods :
• Paramerized constructor to initiaze the data members n
• To accept the details of the student s
• To compute the average & ‘the maximum out of 3 subjects
• Display all the variables
Questions
• Difference Constructor vs method
• Difference static vs non static data members
• Features of constructor
• Constructor overloading
• Two types of constructors ( define)