| [ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ] |
"Object orientation provides a new paradigm for software construction. In this new paradigm, objects and classes are the building blocks, while methods, messages and inheritance produce the primary mechanisms".-Ann L. Winbald, Samuel D. Edwards and David R. King in the book, "Object Oriented Software".Object-oriented programming is a programming method that combines data and instructions into a self-sufficient "object". In an object-oriented program, the design is represented by objects. Objects have two sections, fields (instance variables) and methods. Fields represent what an object is. Methods represent how an object is used. These fields and methods are closely tied to the real world characteristics and use of the object. An object is used by means of its methods. The following figure shows a view of object oriented programming.
Some of the buzzwords in object oriented programming are
* Objects * Classes * Encapsulation
* Abstraction * Inheritance * Polymorphism
We summarize these terms below.Objects
Objects are the basic entities in an object-oriented system. Each object is denoted by a name and has state. The variables within the object express everything about the object (state) and the methods specify how it can be used (behavior). For example, a bicycle has a state (the number of wheels and the current gear), and behavior (braking, accelerating, slowing down, changing gears). So what is a method? A method is a function or procedure that has access to the internal state of the object needed to perform some operation.
Classes
A class is a blueprint or prototype that defines the variables (data) and methods (code) common to all objects of a certain kind. The class serves as a user defined type. A class thus defines a data type that behaves like the built-in types of a programming language. An object is an individual instance of a class.
Access to the variables and methods declared in a class depend on whether they are declared private or public. A variable declared as public is visible everywhere. A variable declared private is not visible outside the class.
Encapsulation
One of the fundamental principles of object technology is that the internals of an object are private to that object and may not be accessed or even examined from outside the object David Taylor, Author, Business Engineering with Object Technology (Wiley, 1995).
Encapsulation is a simple yet reasonably effective building tool. It is a way of packaging information. Encapsulation allows the programmer to present clearly specified interfaces around the services they provide. The programmer can decide what should be hidden and what is intended to be visible. Some advantages of encapsulation are:
Abstraction
- Managing complexity
- Managing change
- Protecting data
Abstraction refers to the act of representing essential features without including the background details or explanations. It is the art of concentrating on the essential and ignoring the non-essential. Classes that use the concept of data abstraction are known as "abstract data types". Abstract data types are achieved by making certain variables and methods in a class private.
Inheritance
“Children classes can enhance the behavior of their parents. And if parental behavior is enhanced, it is to the benefit of the children” –Steve Jobs, CEO, NeXT (February, 1995)
Classes embody coherent and cohesive concepts. Classes can be used to create more specialized classes via inheritance. For example, a bicycle can be specialized to a kind built by a given manufacturer.
Inheritance is the mechanism that allows the programmer to derive new classes from existing classes. The derived classes inherit the methods and data structures of the parent class. One can add new methods or override the inherited methods to make them more specific. The parent methods will not be affected because of these modifications.
For example, different kinds of bicycles like mountain bikes, racing bikes, and tandems can inherit the common properties from a bicycle class and can override the methods in the parent class to suit local properties. For example, some mountain bikes have an extra set of gears with a lower gear ratio. The following figure shows the inheritance.
Bicycle
![]()
Mountain bike Racing bike TandemsPolymorphism
Polymorphism is a feature that allows one interface to be used for a general class of actions. The concept of polymorphism is often expressed by the phrase “one interface, multiple methods”. It basically refers to the ability of giving the same name to methods in different subclasses. The individual methods may implement similar tasks but can be differentiated by the type of arguments passed to them.
Consider a method to print all the properties of a bicycle. There will likely be a different print method for each of the types of bicycles above. By using polymorphism different methods can be created with the same name but with different parameters.
[ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ]