Syntax

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 7.  Inheritance


As previously discussed, the term used for deriving a subclass from a super class is extending because of the Java keyword extends. The formal syntax for the extends keyword is as follows:

 [public] [qualifiers] class subclass extends superclass 
  • public this can have the value public or no value, which identifies package access and is discussed later

  • qualifiers valid qualifiers are final, abstract, static, or nothing at all

Final Classes

Defining a class to be final means that it cannot be subclassed you cannot extend it. Use the keyword final to declare a class to be final, for example:

 public final class MyClass {  } 

Typically, you'll want to define classes to be final when you do not want anyone else extending them. This can be either when they encapsulate all the functionality they will ever need, or if they contain core pieces of functionality that you do not want to expose for use by anyone else in any other capacity than how you intend.

Abstract Classes

Defining a class to be abstract means that it must be subclassed it cannot be instantiated on its own. An abstract class is defined using the abstract keyword, for example:

 public abstract class Car {  } 

Abstract classes typically contain at least one abstract method. Abstract methods are identified by the abstract keyword in their declaration, and they do not contain a method body. Instead, a semicolon terminates the declaration, for example:

 public abstract void accelerate(); 

The Car class might be declared to be abstract because you cannot create a generic car. You must have a specialization of a car, and some methods do not have any good default implementation. The Car class, for example, should not provide a default implementation for the accelerate() and decelerate() methods because they are too specific to the subclasses that implement them. Listing 7.6 defines a new version of the car class that is abstract: Car2.

Listing 7.6 Car2.java
 public abstract class Car2   {      // Transmission types      public static final int AUTOMATIC = 0;      public static final int TIPTRONIC = 1;      public static final int MANUAL = 2;      // Attributes      protected String typeOfCar;      protected int horsePower;      protected int maximumSpeed;      protected int numberOfDoors;      protected String paint;      protected int gasCapacity;      protected int oilCapacity;      protected int transmission;      // State attributes      protected boolean running = false;      protected int currentSpeed;      protected int currentGas;      protected int currentOil;      public Car( String typeOfCar,                  int horsePower,                  int maximumSpeed,                  int numberOfDoors,                  String paint,                   int gasCapacity,                  int oilCapacity,                  int transmission )      {          this.typeOfCar = typeOfCar;          this.horsePower = horsePower;          this.maximumSpeed = maximumSpeed;          this.numberOfDoors = numberOfDoors;          this.paint = paint;          this.gasCapacity = gasCapacity;          this.oilCapacity = oilCapacity;          this.transmission = transmission;      }      public void start()      {           running = true;      }      public void stop()      {          running = false;      }      public boolean isRunning()      {          return running;      }      public abstract void accelerate();      public abstract void decelerate();      public int getCurrentSpeed()      {          return currentSpeed;      }      public String toString()      {          return typeOfCar;       }  } 

Car2 cannot be instantiated itself and all classes that subclass it must either provide an implementation of both the accelerate and decelerate methods or be declared to be abstract. If you declare a class that extends the Car2 class and do not implement both accelerate and decelerate, a compilation error will result. Consider Listing 7.7, it defines a new class: BadCar, that extends Car2 and does not implement its abstract methods.

Listing 7.7 BadCar.java
 public class BadCar extends Car2  {  } 

Upon compilation you should receive the following error:

 BadCar.java:1: BadCar should be declared abstract; it does not                 define decelerate  () in Car2  public class BadCar extends Car2         ^  1 error 

Access Specifiers

In previous chapters you have been exposed to two access modifiers: public and private. This chapter has presented a new hybrid access modifier: protected. public methods and variables are available both from within the class and outside the class. private methods and variables are only available from within the class and not from the outside protected methods and variables are available within the class and within any subclass, but not outside the class.

The end result of this is that if you declare your methods and variables to be public, they will be available to anyone who wants to use them, including your subclasses. If you define them to be private, they will not be available to anyone outside your class, including your subclasses. So, Java defined an access modifier specifically to support data that you want available in subclasses, but nowhere else: protected.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

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