Pages

Tuesday, September 27, 2016

An Abstract Point

Abstract Point | Point No. 1Point No. 2

Explaining objects and classes can be challenging. In a YouTube video titled “Thoughts for students” James Gosling, known as the father of the Java programming language, says that teaching object-oriented programming can be more difficult when students have some programming experience but only with non-object-oriented programming languages like Cobol than when students have no programming experience at all.

In any case, I shall try again to explain objects and classes by way of a series of simple examples. Observe (and code along!) as I attempt to make my first point...

An abstract description of an abstract point


Consider the concept of a point defined in terms of some number of dimensions. A particular type of point, in contrast to the abstract notion of a point, may be a point defined in terms of the variables x and y that is represented by the symbols (x, y) signifying an ordered pair. And an example of an instance of such a point is the ordered pair (3, 2).

No matter how many dimensions we use to define a point, we can think about the notion of a point's distance from the origin.

A JavaScript description of an abstract point


In JavaScript, we can define a new type or class of objects using:
  • a constructor function (note that functions are objects in JavaScript!)
  • a prototype object associated with the constructor function/object

For example, we can define a new type/class called Point like this:
function Point() {};
Point.prototype = {
  constructor: Point,
  distanceFromOrigin: function() {
    throw new Error("Not implemented");
  },
};
Then we can create an instance of our Point type like this:
new Point();
Or like this:
new Point;
Here's what I see when I execute some of the JavaScript code snippets above on the command line in the console of the Chrome web browser that I"m using to compose this post:


When I expand the Point object shown above and expand the Point's __proto__ object, this is what I see:


Now I can declare a variable p and set p equal to the value returned by new Point(); And then I can call the method p.distanceFromOrigin(); as follows:


Note that when a Point object is created using the new keyword as shown above, the contents of Point.prototype become the contents of the newly created object's __proto__ object. Furthermore, JavaScript objects inherit methods from their __proto__ objects. Therefore, in the particular example shown above, I can invoke distanceFromOrigin directly via the statement p.__proto__.distanceFromOrigin(); or indirectly via the statement p.distanceFromOrigin(); as follows:


A Java description of an abstract point


In Java, we can describe an abstract point in a way that is analogous to the JavaScript technique shown above. In the JavaScript code above we attached an object named prototype to a constructor named Point. In Java we'll attach a constructor named Point to a class named Point instead:
class Point {
  Point() {
  }
  double distanceFromOrigin() {
    throw new Exception("Not implemented");
  }
}
Then, as we did in the JavaScript code above, we can declare a variable named p (of type Point) and set the value of p equal to the object returned as a result of involving the Point constructor preceded by the keyword new:
Point p = new Point();
Here's a complete Java program that incorporates that Java code snippets above and can be compiled and interpreted:
class Point {
  Point() {
  }
  double distanceFromOrigin() throws Exception {
    throw new Exception("Not implemented");
  }
}
class TestPoint {
  public static void main(String[] args) {
    try {
      Point p = new Point();
      double d = p.distanceFromOrigin();
      System.out.println(d);
    }
    catch (Exception e) {
      System.out.print(e);
    }
  }
}
Alternatively, the Point class and the method distanceFromOrigin can be declared to be abstract, in which case the Java compiler will not allow the class to be instantiated:
abstract class Point {
  Point() {
  }
  abstract double distanceFromOrigin();
}

Abstract Point | Point No. 1Point No. 2