15.17 ABSTRACT CLASSES IN JAVA


15.17 ABSTRACT CLASSES IN JAVA

Chapter 3 presented the reasons for why abstract classes are important in object-oriented programming. And, Section 15.12 of this chapter discussed how abstract classes are represented in C++.

As mentioned in Section 15.12, a C++ class is abstract if it possesses at least one pure virtual function. As the reader will recall, a member function in C++ must be declared virtual if it is to behave polymorphically. If a virtual member function is also pure, then that function is an abstract function—meaning that no implementation code is provided with the function declaration. The compiler expects to see the implementation code for such a function in a derived class.

In Java, on the other hand, polymorphic behavior is guaranteed for all member functions. So no special virtual-like designation is needed to get a member function to behave polymorphically in Java. However, we still need in Java a declaration that would correspond to the designation of a function as "pure virtual" in C++. Java's term for doing the same is abstract. When a function header includes the keyword abstract, that means an abstract function is being declared, without implementation code. The compiler would expect to see the implementation code for such a function later in a derived class. A Java class with at least one abstract function is abstract.

In Java, abstract classes must be declared so explicitly. (Contrast this with C++ where for a class to be considered abstract, although it must possess at least one function that is pure virtual, no explicit declaration of ‘abstractness’ is required in the class header.) Therefore, our definition of the Shape class of Section 15.12 would now be written as

      abstract class Shape {          abstract public double area();          abstract public double draw();          //....      } 

If a class has at least one abstract method, the compiler thinks of that class as abstract and demands that the keyword abstract appear in the header of the class. However, a class can be abstract even without possessing any abstract methods (another point of difference from C++). We will show an example of such a class at the end of this section.

Note we used the access modifier "public" for the two abstract methods in the above example. As far as the Java compiler is considered, the only restriction on the access modifier is that it cannot be "private" because that would make no sense for an abstract method, since it has to be inherited by a derived class where its implementation code is provided.

As was the case with C++, if a class derived from an abstract base class does not provide implementations for all of the abstract methods of the base class, the derived class is also considered to be abstract. Java demands that such a derived class be declared abstract in the class header. The following source code, which is the Java version of the C++ implementation shown in Section 15.12, illustrates this. We have declared the classes Polygon and CurvedShape as abstract as they both are derived from the abstract class Shape and they both do not provide implementations for the abstract methods of Shape.

 
//AbstractShape Incremental .java abstract class Shape { abstract protected double area(); abstract protected double circumference (); } abstract class Polygon extends Shape { protected int numVertices; protected boolean starShaped; } abstract class curvedShape extends Shape { abstract public void polygonalApprox(); } class Circle extends curvedShape { protected double r; protected static double PI = 3.14159; public Circle () { r = 1.0; } public Circle (double r) { this.r = r; } public double are () { return 2 * PI*r*r; } public double circumference () { return 2 * PI * r; public double getRadius(){return r;} public void polygonalApprox() { System.out. println( "polygonal approximation code goes here"); } } class Rectangle extends Polygon { double w, h; public Rectangle () { w=0.0; h = 0.0; numVertices = 0; starShaped = true; } public Rectangle (double w, double h) { this.w = w; this.h = h; numVertices = 4; starShaped = true; } public double area() { return w * h; } public double circumference () { return 2 * (w + h); } public double getWidth() { return w; } public double getHeight() { return h; } } class Test { public static void main(String [] args) { Shape [] shapes = new Shape [ 3 ]; shapes [0] = new Circle (2.0); shapes [1] = new Rectangle (1.0, 3.0); shapes [2] = new Rectangle (4.0, 2.0); double total_area = 0; for (int i=0; i < shapes. length; i++) total_area += shapes [i] .area(); System. out .println("Total area = " + total_area); } }

The class diagram shown in Section 15.12 also applies to the code shown above.

As already mentioned, a Java class can be abstract even if it does not have any abstract methods, provided that the class is declared to be abstract. An example of such a class is the adapter[14] class WindowAdapter that we will present in Chapter 17 on graphics programming in Java. Its implementation is

      public abstract class WindowAdapter implements WindowListener {          public void windowActivated(WindowEvent e) {};          public void windowClosed(WindowEvent e) {};          public void windowClosing( WindowEvent e) {};          public void windowDeactivated(WindowEvent e) {};          public void windowDeiconified(WindowEvent e) {};          public void windowIconified(WindowEvent e) {};          public void windowOpened( WindowEvent e) {};      } 

Note that every method has a do-nothing implementation. Also note that since the class is abstract, it does not need a constructor and therefore none is defined.

With regard to abstract methods, Java has one feature not possessed by C++: In a class hierarchy, any method inherited from any of the superclasses can be declared to be abstract, making that particular class in the hierarchy an abstract class. This can be useful when it is necessary to block the inherited definition of a method to one or more derived classes in a class hierarchy. Blocking inheritance in this manner was presented earlier in Chapter 3.

[14]As presented in Chapter 17, an adapter class in Java is a trivial implementation of an interface, trivial in the sense that all of the methods declared in the interface are given do-nothing implementations. Adapter classes are defined for purely programming convenience. Java interfaces, introduced earlier in Chapter 3, are presented more fully in the next section.




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