Class as a Reference Data Type


It has already been said that a class is also a data type- a non primitive reference data type. This was very much implicit in the way we had declared a class variable in the following way.
Student s;
This declaration was identical to the way we declared the int variable firstNumber was declared in our Addition program
int firstNumber;
Now, considering this similarity, it should be possible to use a class everywhere we had used a primitive data type such as int. We can have instance variable of a class type, we can accept parameters of class type and we can also return objects from methods. All that needs to be done is to specify the particular reference type (the class name) wherever we had earlier specified the data type as int or double.
We shall now look into each of these possibilities and their applications.
The first in queue is to declare an instance variable of a class type. Assume that we have a Tutor class which would represent a person who would be taking classes for Students and each Tutor will be taking classes for exactly two Students. So, we need to have two Student instance variables in a Tutor class in the following way:
public class Tutor {
   Student s1,s2;
}
All the things that we have learnt with declaring and initialising variables of simple data types are applicable here also. And now, we need a constructor for this Tutor class which will initialise the variables s1 and s2 with two objects that would be passed as arguments. The constructor would be having the following form.
public Tutor ( Student stu1, Student stu2 ) {
   s1 = stu1;
   s2 = stu2;
}
We can also have methods that would return Student objects. For example, consider the following method getS1 which returns the s1 object. note that this method is however not necessary here as s1 hasn’t been declared to be private.
public Student getS1() {
   return s1;
}
Note that we have specified the return type to be Student just like int, double and void. And lastly, we have to deal with referencing the constituents of an object. Suppose that we have a Tutor object named t. How would we call the print Details() method of s1? It is very much the same thing that we have been doing now with a little addition.
t.s1.printDetails();
t.s1 gives us access to the first Student’s object and then we invoke the print Details() method on that object. Here is a small class for Tutor and a test program which follows it which illustrate all that has been told till now.
public class Tutor {

Student s1, s2;

Public Tutor ( Student st1, st2 ) {
      s1 = st1;
      s2 =st2;
   }
}

public class TutorTest {

Public static void main ( String args[] ) {
      Student student1 = new Student ( “Gautham”, 3 );
      Student student2 = new Student ( “Sai”, 4, 98, 100, 96);
      Tutor t = new Tutor (s1,s2);
      t.s1.printDetails();
      t.s2.printDetails();
   }
}

Run the above program and try to understand the output.
There are many more possibilities that we can try here. One such possibility is to check which of the two Students of a particular Tutor is performing better by considering his average. For this we can provide a compareStudents() method within the Tutor class itself in the following way. We have used the decision making statement if- else that has been introduced earlier.
public void compareStudents() {
   if ( s1.getAverage() >= s2.getAverage() ) {
      System.out.println(“s1 is better”);
   } else {
      System.out.println(“s2 is better”);
   }
Note that this method doesn’t give the correct output if both of them have the same average. We will see later on how we can do it. And apart from this we can also provide an another method comapreToTutor() which would compare a Tutor to another Tutor that would be passed as an argument based on the sum of average marks of both the students.
public void compareToTutor ( tutor t ) {
   double average1 = s1.getAverage() + s2.getAverage() ;
   double average2 = t.s1.getAverage() + t.s2.getAverage() ;
   if ( average1 >= average2) {
      System.out.println(“Tutor is performing well”);
   } else {
      System.out.println(“Tutor is not performing well”);
   }
}
Observe the computation of average1 and avergae2. Avearge1 is the average of the Tutor on which this method would be called. And average2 is the average of the Tutor that would be passed as parameter.
In the main method of a test class, we write the following code:
Tutor t1 = new Tutor ( new Student ( “Sai”, 3,98,97,100), new Student (“Gautham”,4,94,97,98) );
Tutor t2=new Tutor ( new Student ( “Ravi”, 7, 99, 98, 100), new Student (“Ranjith”, 34, 95, 97,97) );
t1.comapreToTutor(t2);
Note how the Tutor objects were created. Instead of first creating the Student objects and then passing them to the constructor of Tutor, we have written the object creation statements within the parentheses itself. As always, the arguments in the parentheses would be evaluated first and then the constructor would be called. In this case, first the two Student objects would be created and then the Tutor constructor would be called. And then t1 is compared to t2. Run the program and see down the result.
Author: , 0000-00-00