Object-Oriented Programming - Summary
Java is an object-oriented programming language. Instead of building a program around a set of interrelated procedures, you use classes and methods. A Java stand-alone program must define the public static void main method which is the first method to execute when a program starts. The important concepts of Java (and, in fact, of any object-oriented programming language) are:
- The fundamental structure in Java. Classes combine data fields, constructors, and methods into smart objects that can store information and handle it appropriately. Classes should be designed to be as smart as possible; they should be well defined, flexible, and as independent as possible.
- Objects are manufactured according to the blue-print of their corresponding classes. They relate to classes in the same way that an actual existing house relates to the blueprint describing the house. Objects exist in the computer's memory.
- The process of manufacturing an object from its class blueprint. At the time an object is instantiated, memory is allocated and the class is initialized, using the class constructor. Instantiating is done with the new keyword.
- Language construct that allows for classes to be related to one another so that a subclass can inherit features of its superclass. Subclasses always inherit all non-private fields and methods of their superclass. Mmultiple inheritence is not possible in Java (but an alternative mechanism is available). Classes use inheritance via the extends keyword. The methods of the superclass are available via the super keyword and as a matter of course the superclass' constructor should be invoked as the first line of the subclass' constructor.
- Language construct that enables programmers to limit access to parts of a class. Every class could have public, private, and friendly fields and methods, using the keywords private, public, or no modifier for friendly access. Entire classes can also be declared private, public, or friendly (although a private class probably does not make much sense).
- The ability to use one method name more than once. The only requirements are that the type or number of arguments are different for methods with the same name. Methods with the same name should always have similar functionality. The selection of the correct method is based on the type or number of the arguments in the calling expression.
- Let's a subclass redefine a method that has been previously defined by a superclass. For this to work, the subclass' method must have exactly the same name and number and type of parameters as the method to be overridden.
- Polymorphism - skipped
- Ability to let the compiler select the appropriate method automatically when a call is made via a superclass reference. Polymorphism always requires a class hierarchy where often a superclass has an abstract method that is implemented in the various subclasses. When calling the superclass' abstract method, the compiler will choose automatically the appropriate method based on the actual type of the object.
Of course, Java has the usual standard variable types such as double, int, char, String, and boolean (but no Pointers), if-then-else contol, loop structures, array structures, and more. The actual language is closely related to C++.