In this post we will be learning about the concepts of Accessor-Mutator and Constructors in Java.
Accessor and Mutator
Accessor and Mutator are basically Getter and Setter methods. This is a way of implementing Encapsulation which is a key feature of Object Oriented Programming.
An Accessor method is used to return the value of a private member of the class. Similarly a Mutator method is used to set a value of a private member.
An instance of an object is created using the new keyword. The private member values are initialised using the Mutator method.
Constructor
The constructor can be said to be a special method that has the same name as the class name.
The constructor is used to initialise the values of the attributes of an object. The constructor is implicitly invoked when an object is created. If a constructor is not defined for a particular class the compiler provides a default constructor. It calls the default parent constructor and initializes all instance variables to default values ie. zero for numeric values, false for boolean values, etc. There is no return type or return statement in the body of a constructor. Constructors can be overloaded.
Given below is an example of a constructor.
class Student
{
//data members
private int GR_no;
private CPI;
//default Constructor
public Student()
{
GR_no = 1;
CPI = 0;
}
//parametrized Constructor
public Student(int n,int c)
{
GR_no = n;
CPI = c;
}
public static void main(String args[])
{
Student s1 = new Student(); //no argument constructor will be called.
Student s2 = new Student(1710,9.5); //Parameterized Constructor
}
}
The example shows the use of default and parametrised constructor in Java.
Smruti Khire
K-38
Accessor and Mutator
Accessor and Mutator are basically Getter and Setter methods. This is a way of implementing Encapsulation which is a key feature of Object Oriented Programming.
An Accessor method is used to return the value of a private member of the class. Similarly a Mutator method is used to set a value of a private member.
An instance of an object is created using the new keyword. The private member values are initialised using the Mutator method.
Constructor
The constructor can be said to be a special method that has the same name as the class name.
The constructor is used to initialise the values of the attributes of an object. The constructor is implicitly invoked when an object is created. If a constructor is not defined for a particular class the compiler provides a default constructor. It calls the default parent constructor and initializes all instance variables to default values ie. zero for numeric values, false for boolean values, etc. There is no return type or return statement in the body of a constructor. Constructors can be overloaded.
Given below is an example of a constructor.
class Student
{
//data members
private int GR_no;
private CPI;
//default Constructor
public Student()
{
GR_no = 1;
CPI = 0;
}
//parametrized Constructor
public Student(int n,int c)
{
GR_no = n;
CPI = c;
}
public static void main(String args[])
{
Student s1 = new Student(); //no argument constructor will be called.
Student s2 = new Student(1710,9.5); //Parameterized Constructor
}
}
The example shows the use of default and parametrised constructor in Java.
Smruti Khire
K-38
