Abstract Point | Point No. 1 | Point 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:
Then we can create an instance of our Point type like this:function Point() {}; Point.prototype = { constructor: Point, distanceFromOrigin: function() { throw new Error("Not implemented"); }, };
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:new Point;
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:
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:class Point { Point() { } double distanceFromOrigin() { throw new Exception("Not implemented"); } }
Here's a complete Java program that incorporates that Java code snippets above and can be compiled and interpreted:Point p = new Point();
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: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); } } }
abstract class Point { Point() { } abstract double distanceFromOrigin(); }
Abstract Point | Point No. 1 | Point No. 2