Encapsulation in Action Using Java


Encapsulation is used in a Java program nearly the same way as it is used in a C++ program, except different syntax applies. Let s take a look at how encapsulation works in Java. The next example is the Java version of the C++ program that illustrates the private access specifier .

Two classes are defined in this example. The first class is StudentInfo , which is the Java application class. As you ll recall from your Java application course, a Java application is contained within a class definition called the Java application class. The Java application class is the entry point to a Java application.

The other class definition is Student . The Student class is the class that defines attributes and methods of a student and has the same purpose as the Student class in the previous C++ examples. We ll use the term method instead of procedure in this section because a procedure in Java is called a method.

Let s begin our exploration with the definition of the Student class. You ll need to understand how the Student class works before you can understand how the class is used in the Java application class.

Notice that the Student class resembles the Student class defined in the C++ example. Both class definitions define the same two methods and the same four attributes. These methods also perform the same functionality as methods defined in the Student class of the C++ example.

However, there are three subtle differences in the Java version. Notice that the class definition doesn t have a public access specifier section and a private access specifier section. Instead, the keywords public and private precede the name of the attribute and method to perform the same functionality as the public access specifier section and private access specifier section in the C++ version of this program.

Another difference is that the String data type is used instead of a char array for the student s name. The last difference is the way text is displayed on the screen in the Display() member method. In Java, the System.out.print() method is used to display text on the screen. The System.out.print() method displays text without a carriage return and line feed at the end of the line. We do this because the Display() member method of the GradStudent class definition displays text on the same line as the Display member method of the Student class. You ll see how this is done in the next example:

 class StudentInfo { 
public static void main (String args[]) {
Student myStudent = new Student();
myStudent.Write(10, 1,"Bob","Smith");
myStudent.Display();
}
}
class Student
{
public void Write(int ID, int Grad, String Fname, String Lname) {
m_ID = ID;
m_Graduation = Grad;
m_First = Fname;
m_Last = Lname;
}
public void Display(){
System.out.println( "Student: " + m_ID + " " + m_First + " " +
m_Last + " Graduated: " + m_Graduation);
}
private int m_ID, m_Graduation;
private String m_First;
private String m_Last;
}

Protected Access Specifier in Action

The protected keyword is used in a Java class definition to tell the computer that another class can inherit an attribute or member method. This is nearly identical to the protected access specifier used in the C++ example.

The following is the Java application version of the C++ example that defines a Student class and a GradStudent class, shown previously in this chapter. As you probably suspect, there are subtle differences between the two programs. We ll explore those differences here.

The following example declares three classes. The first two class definitions are the same as in the example of the private access specifier. The third class definition is new. It is the definition of a graduate student and is called GradStudent . The GradStudent class inherits attributes and member methods of the Student class by using the keyword extends followed by the name of the class it inherits, which is Student in this example.

The Student class definition is nearly the same as the Student class definition used in the private access specifier example, with one exception. Notice that attributes are preceded with the keyword protected , which tells the computer that attributes can be accessed by member methods of the GradStudent class. The GradStudent class definition is the same as the GradStudent class definition in the C++ example.

An instance of the GradStudent class is declared in the main() method of the Java application class definition and is then used to call the Write() method to assign values to attributes of the GradStudent class and attributes inherited from the Student class. Those values are then displayed on the screen by calling the Display() method of the GradStudent class.

Notice that the Display() method of the GradStudent class is slightly different from the Display() method of the Student class. This looks a little confusing, so let s take a closer look, beginning with the Display() method of the Student class.

The Display() method of the Student class displays values of attributes defined in the Student class. The Display() method of the GradStudent class enhances the capability of the Display() method of the Student class by displaying both attributes of the Student class and attributes of the GradStudent class. Here s how this is done: Remember that the GradStudent class inherits public and protected members of the Student class. This means that the Display() method of the GradStudent class can call the Display() method of the Student class, which it does in this example. A class that is being inherited is called a super class , and the keyword super is used to access its attributes and member methods.

In this example, super.Display() tells the computer to call the Display() method of the Student class, which displays attributes of the Student class on the screen. The next statement displays attributes of the GradStudent class on the same lines as attributes of the Student class.

 class StudentInfo { 
public static void main (String args[]) {
GradStudent myStudent = new GradStudent();
myStudent.Write(10, 1,"Bob","Smith", 2000,"Columbia University",
"CS");
myStudent.gradDisplay();
}
}
class Student
{
public void Write(int ID, int Grad, String Fname, String Lname) {
m_ID = ID;
m_Graduation = Grad;
m_First = Fname;
m_Last = Lname;
}
public void Display(){
System.out.print( "Student: " + m_ID + " " + m_First + " " +
m_Last + " Graduated: " + m_Graduation);
}
protected int m_ID, m_Graduation;
protected String m_First;
protected String m_Last;
}
class GradStudent extends Student
{
public void Write(int ID, int Grad, String Fname, String Lname, int
yrGrad, String unSch, String major) {
m_ID = ID;
m_Graduation = Grad;
YearGraduated = yrGrad;
m_First = Fname;
m_Last = Lname;
m_UndergradSchool = unSch;
m_Major = major;
}
public void Display(){
super.Display();
System.out.println( " " + m_UndergradSchool + " " + m_Major +
" " + YearGraduated);
}
int YearGraduated;
private String m_UndergradSchool;
private String m_Major;
};



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net