Object-Oriented Programming (OOP)
OOP–Object Oriented Programming, OOP, is the most influential paradigm of our time. This handout summarizes the most basic style, elements, and vocabulary of OOP that are common to all OOP languages. OOP languages can have weird features, but the basic ideas of OOP are pretty straightforward.
In OOP, the logical arrangement of the code is changed. Instead of an informal arrangement of functions into different files, functionality is officially and tightly grouped with the type that it operates on. The OOP style groups all the operations together according to what they operate on — all the hash table operations are part of the hash table class, all the string operations are part of the string class. Put another way: if variables are nouns and functions are verbs, then OOP divides up everything around the nouns.
ClassThe most basic concept in OOP is the Class. A class is like a type in the classical language. Instead of just storing size and structural information for its data, a class also stores the operations which will apply to the data. Class = Storage +
Behavior. A class is like an Abstract Data Type in Pascal or C— it creates a logical coupling between data and the operations on that data. Bundle the verbs with their nouns.
ObjectAn object is a run-time value that stores the state and belongs to some class. Objects know what class they belong to, and so they automatically know what operations they are capable of. The word “instance” is another word for “object”.
Encapsulation“Encapsulation” refers to protecting the internals of an object from direct manipulation by the client. The client can send messages, but the client cannot change the bits in the object directly. In C, it’s just a convention that the client
should not mess with or depend on the implementation of an Abstract Data Type (ADT). With encapsulation, the compiler enforces the separation. The class implementation can indicate which parts of the implementation are protected so that the client code accessing those parts will not compile. Or put another way: the client cannot mess with the object’s state — the client can only send messages. The object’s state is only touched by its own methods. Once those methods are correct and debugged, the object can be given to any client, and that client should not be able to perform an operation on the object which puts it in an incorrect state.
HierarchyClasses in OOP are arranged in a tree-like hierarchy. A class’ “superclass” is the class above it in the tree. The classes below a class are its “subclasses.” The semantics of the hierarchy is that classes have all the properties of their superclasses. In this way, the hierarchy is generally up towards the root and specific down towards its leaves. The hierarchy helps add logic to a collection of classes. It also enables similar classes to share properties through “inheritance” below. A hierarchy is useful if there are several classes that are fundamentally similar to each other. In C++, a “base class” is a synonym for superclass, and a “derived class” is a synonym for subclass.
Inheritance“Inheritance” is the process by which a class inherits the properties of its superclasses. Methods, in particular, are inherited. When an object receives a message, it checks for a corresponding method in its class. If one is found, it is
executed. Otherwise, the search for a matching method travels up the tree to the superclass of the object’s class. This means that a class automatically responds to all the messages of its superclasses.
OverridingWhen an object receives a message, it checks its own methods first before consulting its superclass. This means that if the object’s class and its superclass both contain a method for a message, the object’s method takes precedence. In
other words, the first method found in the hierarchy takes precedence. This is known as “overriding,” because it gives a class an easy way to intercept messages before they get to its superclass. Most OOP languages implement overriding
based on the run-time class of objects. In C++, run-time overriding is an option invoked with the “virtual” keyword.