Object Oriented Concepts: Commentary

Steven Zeil

Last modified: Sep 11, 2016
Contents:

This section will introduce you to some of the concepts and terminology of Object-Oriented (OO) programming. Although it is not the purpose of this course to teach you why OO is good, or how to actually perform it well, you will need to be familiar with the basic terminology in order to interpret what your read about an OO language like Java.

1 What is an Object?

(Click on the Java icon above to open up or synchronize the related section of the Tutorials.)

There’s always a certain tendency for people reading examples like this to think that the authors are being “cute” by using real-world objects like “bicycles” as their examples.

But, in fact, the very best abstract data types are those that represent real-world objects. Good programmers have understood this for a long time. The OO people have, however, embraced this idea as a kind of fundamental principle.

The most problematic parts of a large software system design are often the places where the programmers and designers allowed their powers of invention free rein, unfettered by any grounding in reality. Those are the parts of the system that need to be explained, repeatedly, to everyone else on the team, to the end users, to managements, etc., because they just don’t conform to peoples’ expectations of how stuff actually behaves. That’s a lot of wasted effort.

So, even though some programmers will chafe at having their “creativity” restricted, “keeping it real” is a potent slogan for good software design.

With that said, can we relate the concept of “object” to more familiar programming terms? An “object” is pretty much what you are used to referring to as a variable or constant in your code.

2 What is a Class?

The Java class is pretty much synonymous with the C++ class.

Java does not have a separate “struct” construct. Remember that, in C++, the only difference between a struct and a class is the default visibility (public/private) of the members. Until you actually say “public:” or “private:”, then members in struct are public and members in a class are private. Once you actually say “public:” or “private:”, then there is no difference between the two at all. A C++ struct can have both data and function members, constructors, etc.

As we’ll see later, Java does not break the class apart into public and private regions, but instead labels the visibility of each member individually. Hence it would not make sense to have two different constructs that differ only by the default visibility of the opening region of the class.

Syntax alert: In Java there is no ‘;’ at the end of a class declaration.

3 What is Inheritance?

If you aren’t familiar with inheritance in C++ yet, don’t worry too much about this yet. But the concept is still a useful one.

Slight syntax change. The mountain bike class in C++ would be written:

class MountainBike : public Bicycle {
   // new fields and methods defining a mountain bike would go here
};

4 What is an Interface?

C++ has no direct equivalent to the Java interface.

However, C++ does something that is pretty much equivalent by using inheritance coupled with the idea of an abstract class:

class Bicycle {
public:
   void changeCadence(int newValue) = 0;   // wheel revolutions per minute

   void changeGear(int newValue) = 0;

   void speedUp(int increment) = 0;

   void applyBrakes(int decrement) = 0;
};

If you have never encountered an abstract class in C++, don’t worry about it for now.

We’ll look later at why Java uses a separate language construct for this.

5 What is a package?

The opening sentence says “A package is a namespace that…”, which pretty much clues you in that the equivalent C++ construct is the namespace.

Most C++ students don’t have a whole lot of experience creating their own namespaces and putting classes into a namespace. It’s not hard:

namespace MyTransportation {

class Bicycle {
public:
   int cadence = 0;
   int speed = 0;
   int gear = 1;

   void changeCadence(int newValue) {
      cadence = newValue;
   }

   void changeGear(int newValue) {
      gear = newValue;
   }

   void speedUp(int increment) {
      speed = speed + increment;
   }

   void applyBrakes(int decrement) {
      speed = speed - decrement;
   }

   void printStates() {
      cout << "cadence:" << cadence 
           << " speed:"<< speed << " gear:" << gear
           << endl;
   }
};

class MountainBike<span class="emph">: public Bicycle</span> {
   // new fields and methods defining a mountain bike would go here
};

On the other hand, most C++ students have a lot of experience working with one namespace in particular, the std namespace containing all of the elements of the C++ standard library.

Namespaces and packages fulfill the same role: they allow us to group related items together, separating them from the rest of our world of code. This has the drawback that the items now have longer names (MyTransportation::Bicycle) but it has the advantage that we don’t need to worry about the possibility of a distinct class named “Bicycle” existing elsewhere in the code, causing a naming conflict. And the awkwardness of the longer names can be ameliorated by short cuts permitted by the language (e.g., the using statement in C++). We’ll see similar shortcuts later in Java.