Object Oriented Programming
Object oriented programming is basically like imperative programming,
but data objects and the operations performed on them are tied together
more closely:
- Objects with the same structure and behavior are grouped into
classes
Classes are similar to structs in C or records in Pascal.
The main difference is that the methods that apply to objects are
part of the object, whereas with structs or records they are typically
not.
- Some of the data fields of an object may be publicly visible,
but most data fields can normally only be inspected and modified by
calling (instance) methods of the object.
In a very "strict" interpretation of object oriented programming
data fields are never visible, so they always need to be accessed through
methods. JavaBeans for instance requires that all data be
accessed through get and set methods.
- Some operations are related to a class but do not "operate on" an
object of the class. They are called class methods.
A typical example of such methods is the constructor of a class.
It cannot be an instance method because the instance it would apply to
still has to be created when the method is called.
- A class can be extended by adding data fields and methods.
The extended class is called a subclass, the original one the
superclass.
The data and methods from the original class are inherited
by the subclass.
- When a class extends only one superclass we have single inheritance.
- When a class extends several superclasses there is multiple
inheritance.
Not all object oriented programming languages allow multiple inheritance.
Some (like Java) allow multiple inheritance of definition but only
single inheritance of implementation.
- One can define a class without implementing any data fields or methods.
They are sometimes called interfaces.
- One can define a class and implement only some data fields or methods.
They are called abstract classes.