Documente Academic
Documente Profesional
Documente Cultură
Oriented Programming
1
[Mossenbock 1993] Hanspeter Mossenbock, Object-Oriented Programming in
Oberon-2, Springer-Verlag, 1993.
60 Tips for Object Oriented Programming
CLASS
OBJECTS
A class contains both data and functions operating on it. Member func-
tions are known as methods and data members are known as fields.
When a method is invoked on an object, it is said that the object is
passed with a message.
The members (function or data) that are common to all the objects of
a class can be provided in the class itself. Such class members are also
known as static members (static functions or static data). For example, a
class might need to keep track of the number of objects created from
that class. A static data member can be used as a counter that will be
incremented or decremented whenever a new object is created or de-
stroyed. A function that is used to increment, decrement or access the
value of that counter will be a static function.
ACCESS SPECIFIERS
are not meant to be exposed to users, and contains just the implementa-
tion detail of class that users are not concerned about. Public members
are designed and supposed to be used by the users of the class. It is
through the public members that the class is accessed and used; these
members are collectively referred to as the public interface of the class.
USERS OF A CLASS
Encapsulation
Abstraction
Abstraction means hiding lower-level details from the users and expos-
ing only the essential and relevant details to the users.
For example, for driving a car, it is enough for the driver to know
essential and relevant details such as knowledge and use of steering,
gear, dash board, breaks, accelerator, etc. It is not necessary to know the
internal implementation details such as how spark plugs work, how fuel
is injected into the combustion chamber, or how batteries are charged
indirectly by the engine. Essentially, as a driver, we are concerned only
about using the car instead of worrying about how the car works and
provides the functionality. In other words, a car abstracts the internal
details and only relevant details are exposed to us as a driver of the car.
In object oriented programming, a class (specified by the keyword
class in C++/Java/C#) specifies an Abstract Data Type (ADT). A class is
the basic unit of abstraction; it has abstract meaning, providing a higher-
level view of the operations to the users and hides the lower-level im-
plementation details. In other words, a class lets us focus exclusively on
behaviour and lets us ignore its internal representation.
Inheritance
Polymorphism
parameters used, the compiler will resolve the call to the correct method.
In operator overloading, the predefined operators defined by the lan-
guage can be supported by the classes also. In this way, classes can
behave like primitive types (like int, float etc) defined by the language.
Depending on the arguments used for the operator, correct method will
be resolved by the compiler.
Runtime polymorphism provides the basis of reusability in object ori-
ented programming. In inheritance, a base class pointer or reference can
hold a derived class object. We can invoke methods from the base class
pointer or reference. If such a method is a virtual method, then the
actual method invoked depends on the dynamic type of the object pointed
by the base class pointer or reference. The type of the base class pointer
or reference is known as the static type of the object, and the actual
runtime type of the actual object pointed by the pointer or reference is
known as the dynamic type of the object.
When the compiler sees invocation of a method from a base class
pointer or a reference, and if the method is a virtual method, it defers
determining the exact method to be called to runtime. At runtime, based
on the actual dynamic type of the object, the method is invoked. This
mechanism is known as dynamic method resolution or dynamic method
invocation.
There are some classes that are used only for abstraction/encapsulation
and provide a specific functionality; they do not also make use of inher-
itance/virtual functions. Classes that do not have any virtual functions
(runtime polymorphism) are known as monomorphic classes.
The classes that have virtual functions and designed for making use of
runtime polymorphism are known as polymorphic classes.
Classes are modelled based on the real world objects and their relation-
ships. Sometimes, there are objects that are not really seen in the real-
world, and classes model an abstract relationship between these objects.
& 60 Tips for Object Oriented Programming
For example, Matiz, Ambassador, Indica and Santro are a few car
objects. From these objects, we can model a class named as Car. Simi-
larly, from various bike objects, we can design a Bike class. Both Car
and Bike classes have many common properties, and we can abstract
those common properties as a Vehicle class. This modelling proves
useful in practice. However, Vehicle is an abstract concept and we
cannot see direct instances of a Vehicle class (but there are specific
instances of a Car class). So, Vehicle class is an abstract base class. An
abstract class is a class which cannot be instantiated. The Car and Bike
classes are concrete classes. A concrete class is a class that can be instan-
tiated.
From programming point of view, if definitions of all the methods are
provided in a class, then the class is a concrete class. If there is at least
one virtual method of a class that does not have an implementation (i.e.
definition or body of the method), then it is an abstract class. If all virtual
methods are just declared and no implementation is provided for the
class, then it is an interface class.
An important note: An interface class is sometimes shortly referred to
as an interface (which is also a language feature in Java and C#). This
term should not be confused with the term class interface, which refers
to the public members of a class.
TYPES OF INHERITANCE
Object oriented programming is not a panacea for all the problems faced
by the software industry today. With more than three decades of object
oriented programming, the hype and over-enthusiasm in promoting object
orientation has subsided and the costs of object oriented programming
are now well-known and clear.
60 Tips for Object Oriented Programming