Lesson Set 5 Constructors & Passing Objects To Methods: Purpose
Lesson Set 5 Constructors & Passing Objects To Methods: Purpose
Lab 5
Note: It is called constructor because it constructs the values at the time of object
creation. It is not necessary to write a constructor for a class. It is because java
compiler creates a default constructor if your class doesn't have any.
2. Parameterized constructor
Syntax
1. class Bike1{
2. Bike1(){System.out.println("Bike is created");}
Example 3. public static void main(String args[]){
4. Bike1 b=new Bike1();
5. }
6. }
Purpose Default constructor is used to provide the default values to the object like 0, null
etc. depending on the type.
1. class Student3{
2. int id;
3. String name;
4.
5. void display(){System.out.println(id+" "+name);}
6.
7. public static void main(String args[]){
8. Student3 s1=new Student3();
9. Student3 s2=new Student3();
10. s1.display();
11. s2.display();
12. }
}
Parametrized
Constructor A constructor which has a specific number of parameters is called parameterized
constructor. Parameterized constructor is used to provide different values to the
distinct objects.
Example In this example, we have created the constructor of Student class that have two
parameters. We can have any number of parameters in the constructor.
1. class Student4{
2. int id;
3. String name;
4.
5. Student4(int i,String n){
6. id = i;
7. name = n;
8. }
9. void display(){System.out.println(id+" "+name);}
10.
11. public static void main(String args[]){
12. Student4 s1 = new Student4(111,"Karan");
13. Student4 s2 = new Student4(222,"Aryan");
14. s1.display();
15. s2.display();
16. }
Output }
111 Karan
222 Aryan
Passing Objects
Although Java is strictly pass by value, the precise effect differs between whether
a primitive type or a reference type is passed.
When we pass a primitive type to a method, it is passed by value. But when we
pass an object to a method, the situation changes dramatically, because objects
are passed by what is effectively call-by-reference. Java does this interesting
thing that’s sort of a hybrid between pass-by-value and pass-by-reference.
Basically, a parameter cannot be changed by the function, but the function can
ask the parameter to change itself via calling some method within it.
While creating a variable of a class type, we only create a reference to an
object. Thus, when we pass this reference to a method, the parameter that
receives it will refer to the same object as that referred to by the argument.
This effectively means that objects act as if they are passed to methods by
use of call-by-reference.
Changes to the object inside the method do reflect in the object used as an
argument.
In Java we can pass objects to methods. For example, consider the following
Example program :
// Java program to demonstrate objects
// passing to methods.
class ObjectPassDemo
{
int a, b;
ObjectPassDemo(int i, int j)
{
a = i;
b = j;
}
// driver class
public class Test
{
public static void main(String args[])
{
// creating a box with all dimensions specified
Box mybox = new Box(10, 20, 15);
Lab 5.1 Copy the code, run and compile in JAVA IDE and paste the output in the space
given below:
// Java program to demonstrate objects
// passing to methods.
class ObjectPassDemo
{
int a, b;
ObjectPassDemo(int i, int j)
{
a = i;
b = j;
}
Lab 5.2
Write a program that creates a class called CUBE. The data members of the
class are length, width height. Create constructor that will set the individual
values. In the end, create a function that will display all the data members.
Lab 5.3 Write a class called rectangle. Your task is to store the length and width of the
rectangle. Create constructor that will set the individual values taken as input
by the user. Finally write a function that will check if two objects are equal by
comparing the length and width, this function will take another rectangle as
parameter. Demonstrate the use of the object in the main function. Make sure
that the function names are meaningful and self-descriptive.
Lab 5.4 Write a program that creates a class called Student. Your class will have data
members namely name, cmsid, cgpa, array of marks. You will need to design
three functions as follows:
Default Constructor.
Parameterized Constructor.
Function to copy data of one student to another.
Function to display the student record.
Write a mainClass to demonstrate the usage of Student class. Also use array
of students and call respective methods of student class.