Key Ideas
[Part 5 of 11]


There are five key ideas in this part:

  1. The first is that we can extend any normal class to invisibly gain the code within our own and use the class as the basis of ours:

    public class Point {

       private double x = 0.0;
       private double y = 0.0;

       public void setX (double xIn) {
          x = xIn;
       }

       public double getX() {
          return x;
       }

       public void setY (double yIn) {
          y = yIn;
       }

       public double getY() {
          return y;
       }

    }

    public class Space extends Point {
       private double orbitHeight = 0.0;

       public Space() {
          // Launch pad location...
          setX(28.4556);
          setY(-80.5278);
       }

       public void setOrbitHeight (double orbitHeightIn) {
          orbitHeight = orbitHeightIn;
       }

       public double getOrbitHeight() {
          return orbitHeight;
       }

    }

  2. The second is that we can alternatively sign up to a set of promises about the kind of class we'll be. This is called an Interface. Interfaces frequently also have standard-based static final variables:

    public interface Point {

       public static final double GREENWICH_X = 51.4788;
       public static final double GREENWICH_Y = 0.0;

       public void setX (double xIn);
       public double getX();
       public void setY (double yIn);
       public double getY();

    }

    public class Space implements Point {
       private double x = 0.0;
       private double y = 0.0;
       private double orbitHeight = 0.0;

       public Space() {
          // Launch pad location...
          x = Point.GREENWICH_X + 28.4556;
          y = Point.GREENWICH_Y + -80.5278;
       }

       public void setX (double xIn) {
          x = xIn;
       }

       public double getX() {
          return x;
       }

       public void setY (double yIn) {
          y = yIn;
       }

       public double getY() {
          return y;
       }

       public void setOrbitHeight (double orbitHeightIn) {
          orbitHeight = orbitHeightIn;
       }

       public double getOrbitHeight() {
          return orbitHeight;
       }

    }

  3. Thirdly, you can do a bit of both with abstract classes:

    public abstract class Point {

       public static final double GREENWICH_X = 51.4788;
       public static final double GREENWICH_Y = 0.0;

       protected double x = 0.0;
       protected double y = 0.0;

       public void setX (double xIn) {
          x = xIn;
       }

       public abstract double getX();
       public void setY (double yIn) {
          y = yIn;
       }

       public abstract double getY();
    }

    public class Space extends Point {
       private double orbitHeight = 0.0;

       public Space() {
          // Launch pad location...
          x = Point.GREENWICH_X + 28.4556;
          y = Point.GREENWICH_Y + -80.5278;
       }

       public double getX() {
          return x;
       }

       public double getY() {
          return y;
       }

       public void setOrbitHeight (double orbitHeightIn) {
          orbitHeight = orbitHeightIn;
       }

       public double getOrbitHeight() {
          return orbitHeight;
       }

    }

  4. Fourth, all this is most useful when used with the fact that methods will accept a subclass object in place of a superclass or interface. Once an object is passed into a method, however, it can only be used as if it was that superclass or interface:

    Space iss = new Space();
    iss.setOrbitHeight(370000);
    iss.setX(142.3624934744112);
    iss.setY(-38.05295779081117);
    print(iss);

    public void print (Point pt) {

       System.out.println("X = " + pt.getX());
       System.out.println("Y = " + pt.getY());
       // System.out.println ("Height = " + pt.getOrbitHeight()); // This wouldn't work.

    }

  5. Fifth, UML is easier to understand than code. Which of the above is the following?

    UML