3.5 DEFINING A SUBCLASS IN JAVA


3.5 DEFINING A SUBCLASS IN JAVA

To parallel the C++ discussion in Section 3.4, here is how one can extend the class User and define a subclass StudentUser in Java:

 
class User { private String name; private int age; public User( String str, int yy ) { name = str; age = yy; } public void print() { System.out.print( "name: " + name + " age: " + age ); } }; Class StudentUser extends User { //(A) private String schoolEnrolled; public StudentUser( String nam, int y, String sch ) { //(B) super(nam, y); //(C) schoolEnrolled = sch; } public void print() { //(D) super.print(); //(E) System.out.print( " School: " + schoolEnrolled ); } }

As with C++, we refer to User as a base class or a superclass, and to StudentUser as a subclass, a derived class, or an extended class. Note the following important features of this subclass definition:

  • In the header of the subclass definition in line (A), the phrase StudentUser extends User makes StudentUser a subclass of User, or, equivalently, User is a superclass of StudentUser. The subclass will inherit all the members of the superclass, but only the non-private members of the base class will be directly visible in the functions defined for the subclass, as explained further in Section 3.11.

  • In line (B), the constructor for the subclass is defined as

          StudentUser( String nam, int y, String sch ) {           super(nam, y);           schoolEnrolled = sch;      } 

    Note the invocation super() in line (C) which invokes the constructor for the superclass User. So, as with C++, a part of the job in the subclass constructor is done by the constructor for the superclass. Again paralleling C++, if a superclass constructor is not invoked explicitly via super() or, for the case of Java, if another one of the subclass's constructor is not invoked, then the system will try to invoke the superclass's no-arg constructor. These ideas are discussed in detail in Chapter 15.

  • In the definition of print() for the subclass in line (D)

          void print() {           super.print();           System.out.print( " School: " + schoolEnrolled );      } 

    again part of the work of print() for the subclass is done by calling in line (E) the print() function defined for the parent class. The definition of print() for the StudentUser class overrides the definition of the same function inherited from User for objects of type StudentUser.

3.5.1 A Small Demonstration of Polymorphism in Java

Nothing special needs to be done to demonstrate polymorphism in Java. That is, we do not need to manipulate Java objects in any special way. Neither do we need to give functions any special designators in order to make them behave polymorphically. Recall that in the C++ example in Section 3.4.1, we had to manipulate the objects created there through pointers and had to declare the function print() virtual to demonstrate polymorphism. In Java, all objects are polymorphic and all functions behave polymorphically.

To drive home this point, the following Java program is an exact parallel of the C++ program Polymorph.cc of Section 3.4.1. The classes User and StudentUser are exactly the same as shown earlier in this section. As already mentioned, the print() of the derived class in line (G) overrides the print() of the base class in line (F). We have added a Test class to demonstrate polymorphism. In the main of Test, we first declare an array of three User objects in line (H). The elements of the array are initialized in lines (I), (J), and (K). One of the three elements is actually a StudentUser. When the function print() is invoked on the elements of this array in the loop in lines (L), (M), and (N), the object-specific definitions of print() are automatically used in each case. As a result, the output is the same as shown earlier in Section 3.4.1, that is

      name: Buster Dandy   age: 34      name: Missy Showoff   age: 25   School: Math      name: Mister Meister   age: 28 

Here is the source code for this exercise:

 
//Polymorph.java class User { private String name; private int age; public User( String str, int yy ) { name = str; age = yy; } public void print() { //(F) System.out.print( "name: " + name + " age: " + age ); } } class StudentUser extends User { private String schoolEnrolled; public StudentUser( String nam, int y, String sch ) { super(nam, y); schoolEnrolled = sch; } public void print() { //(G) super.print(); System. out. print( " School: " + schoolEnrolled ); } } class Test { public static void main( String[] args ) { User[] users = new User [3]; //(H) users[0] = new User( "Buster Dandy", 34 ); //(I) users[1] = new StudentUser( "Missy Showoff",25, "Math"); //(J) users[2] = new User( "Mister Meister", 28 ); //(K) for (int i=0; i<3; i++) { //(L) users [i].print(); //(M) System.out.println(); } } }

As was the case with C++, in general, there are restrictions on the syntax of an overriding function in a derived class vis-à-vis the syntax of the overridden function in a base class. These are presented in Chapter 15.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

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