Transcript L3

OOP III: Constructors
What is a constructor?
A constructor is a special method that executes during the creation of
an object.
Any object that is created must have a constructor. If there is no
defined constructor, your compiler will create a default constructor and
execute it.
A default constructor essentially does nothing.
What is a constructor?
The idea behind a constructor is that it sets up your object. It can
initialize values and run code to set up your object in a way that is
desirable.
You can have multiple constructors in one object, separated by the
parameters that they take.
What is a constructor?
class name
{
name() // this is the constructor, it shares the name of the class
{
}
// notice that constructors do not have return types
}
Differences between methods and
constructors
• Constructors do not have return types
• Constructors cannot be called explicitly, they execute when you
instantiate the class.
• Methods inside its own class also cannot call constructors.
• A class can have many constructors definitions.
• Constructor should be public
Constructor Demo
Constructor Parameters
Constructors are separated by their parameters.
That means, you must give it its right parameter for that constructor to
execute.
Parameters must match up for constructors to execute.
Do not use return types in constructors
class name
{
int name() // Constructors have the same name as class
{
return 1;
}
}
Constructors cannot be called explicitly
class name
{
name() // Constructors have the same name as class
{
name();
//you cannot do this
}
void method1()
{
name();
// you cannot do this
}
}
Constructor parameters must line up
class name
{
name(int a, int b)
{
}
}
void main()
{
name one = new name(10,10);
}
If there is not a constructor declared, your
compiler creates a default one.
class name
{
method1(){}
}
void main()
{
name one = new name();
}
Mutator/Accessor Methods
• A mutator method is a method that changes the values of data inside
a class.
• An accessor method is a method that accesses the data in the class.
They’re both just a fancy way of describing a method that does
something.
Mutator Method
class name
{
int x;
int y;
void method1()
{
x = 0;
y = 10;
}
}
Accessor Method
class name
{
int x;
int y;
void method2(int k, int j)
{
k = x;
j = y;
print(x,y);
}
}
class Student
{
static int ID;
final String NAME = “Steven”;
private int age;
Student(int id, int age2) // constructor
{
ID = id;
age = age2;
}
int returnAge() // accessor method
{
return age;
}
void changeID(int x)
// mutator method
{
ID = x;
}
}
Exercise 1
Write a class called Student with the following data
private int ID
private String name;
static String School;
Write a constructor that will take three variables and initialize the three
values.
In void main, declare an instance of Student and give it its parameter.
Exercise 2
Write a class called Student with the following data
private int ID
private String name;
static String School;
Declare a constructor with no parameters but the constructor itself will
ask for input for each data above and assign those values.
Exercise 3
Write a class called Student with the following data
private int ID
private String name;
static String School;
Write a constructor with no parameters but the constructor will call mutator
methods to assign values to each data field above. Then it will call accessor
methods to display those values.
You will need to create a method that assigns values and then a method that
displays values.
Exercise 4
Write a class called Student with the following data
private int ID
private String name;
static String School;
Do not create a constructor. However, create methods that will assign
values to these fields and display them. Declare an instance of your
class in void main and call your methods.