Académique Documents
Professionnel Documents
Culture Documents
Lab 3
3rd week of August, 2017
Before defining the class it should be clear to you for what purpose you are going to create a
class. Now let us see how a class is defined in Java.
class <ClassName>
{
type inst_var1;
type inst_var2;
type inst_varn;
type method_name1 (arguments)
{
// body of method;
}
}
An Example of Student class Definition:
class Student
{
String name;
String course;
int age;
void displayInfo ()
{
System.out.println (Student Information);
System.out.println (Name: +name);
System.out.println (Course: +course);
System.out.println (Age: +age);
}
}
A class defines a new data type. In the above example the class Student now can be used as a
data type to create objects of student class.
3.1.1 Creating Objects
Step 2:
Now you see complete Java program for displaying basic information of the students.
class Student
{
String name;
String course;
int age;
void displayInfo ()
{
System.out.println (Student Information);
System.out.println (Name: +name);
System.out.println (Course: +course);
System.out.println (Age: +age);
}
}
class Student_Info
{
public static void main (String args[])
{
Student stud1 = new Student();
// assigning values to the objects
stud1.name = Ravi;
stud1.course = Java;
stud1.age = 23;
stud1.displayInfo();
}
}
Exercise-3.1.1: Display the output of the above program.
Exercise-3.1.2: Create another object the above program and display its information
Exercise 3.1.3: Write a program to create a CALCULATOR with plus, minus, multiply, and divide
operations. Pass arguments to the functions.
3.2 Constructors
A constructor initializes objects with its creation. It has the same name as the name of the class
inside which it is defined. Once a constructor is defined, it is automatically invoked as soon as
memory is allocated to the object. Constructor does not have a return type unlike other
member functions of the class.
class Student
{
String name;
String course;
int age;
Student() // Definition of a default constructor (without arguments)
{
name = Ravi;
course = Java;
age = 23;
}
void displayInfo ()
{
System.out.println (Student Information);
System.out.println (Name: +name);
System.out.println (Course: +course);
System.out.println (Age: +age);
}
}
class Student_Info
{
public static void main (String args[])
{
// allocates memory to the object and invokes constructor
Student stud1 = new Student();
stud1.displayInfo();
}
}
Exercise 3.2.1: Display the result for the above program.
Exercise 3.2.2: Create another object for the above program and display results.
3.2.1 Parameterized Constructors
In the above example as constructor function named Student() is defined. As this constructor
has no arguments, it is called as default constructor. Every time an object is created, this default
constructor gets invoked automatically. As a result, every object will have the same values. To
avoid this problem, we have parameterized constructors that assign different values to the
different objects.
class Student
{
String name;
String course;
int age;
// Definition of a parameterized constructor
Student(String n, String c, int a)
{
name = n;
course = c;
age = a;
}
void displayInfo ()
{
System.out.println (Student Information);
System.out.println (Name: +name);
System.out.println (Course: +course);
System.out.println (Age: +age);
}
}
class Student_Info
{
public static void main (String args[])
{
//allocates memory to the object and invokes constructor
Student stud1 = new Student(Ravi,Java,23);
stud1.displayInfo();
Student stud2 = new Student(Rex,Java,22);
Stud2.displayInfo();
}
}
3.3 Overloading Constructors
There can be more than one constructor in the class, but every constructor should have a either
different number of arguments or different types of arguments. In this case, the constructor is
said to be overloaded.
class Student
{
String name;
String course;
int age;
// Definition of a parameterized constructor
Student(String n, String c, int a)
{
name = n;
course = c;
age = a;
}
Student(String n, int a)
{
name = n;
course = Java;
age = a;
}
void displayInfo ()
{
System.out.println (Student Information);
System.out.println (Name: +name);
System.out.println (Course: +course);
System.out.println (Age: +age);
}
}
class Student_Info
{
public static void main (String args[])
{
//allocates memory to the object and invokes constructor
Student stud1 = new Student(Ravi,Java,23);
stud1.displayInfo();
Student stud2 = new Student(Rex,22);
Stud2.displayInfo();
}
}
Exercise 3.3.1: Write a program in Java to create Bank_Account class, which defines two
different constructors to create objects.
Exercise 3.3.2: Write a program in Java, in which a method named add is overloaded. The add
method sums two integer values, one integer value, and one double value, two double values.
}
Student(String n, int a)
{
name = n;
course = Java;
age = a;
}
void displayInfo ()
{
System.out.println (Student Information);
System.out.println (Name: +name);
System.out.println (Course: +course);
System.out.println (Age: +age);
}
}
class Student_Info
{
public static void main (String args[])
{
//allocates memory to the object and invokes constructor
Student stud1 = new Student(Ravi,Java,23);
stud1.displayInfo();
Student stud2 = new Student(Rex,22);
Stud2.displayInfo();
}
}