04-12-2012, 06:33 PM
OOP Concepts by Example
OOP.Concepts.pdf (Size: 81.18 KB / Downloads: 111)
Inheritance
Let us start by defining inheritnace. A very good website for finding computer science
definitions is . The definitions in this article are stolen from that
website.
Definition: Inheritance
Inheritance is the concept that when a class of object is defined, any subclass that is
defined can inherit the definitions of one or more general classes. This means for the
programmer that an object in a subclass need not carry its own definition of data and
methods that are generic to the class (or classes) of which it is a part. This not only
speeds up program development; it also ensures an inherent validity to the defined
subclass object (what works and is consistent about the class will also work for the
subclass).
Polymorphism
Inheritance is a very easy concept to understand. Polymorphism on the other hand is
much harder. Polymorphism is about an objects ability to provide context when methods
or operators are called on the object.
Definition: Polymorphism
In object-oriented programming, polymorphism (from the Greek meaning "having
multiple forms") is the characteristic of being able to assign a different meaning to a
particular symbol or "operator" in different contexts.
Encapsulation
The least understood of the three concepts is encapsulation. Sometimes, encapsulation is
also called protection or information hiding. In fact, encapsulation, protection and
information hiding are three overlapping concepts.
Definition: Encapsulation
Encapsulation is the inclusion within a program object of all the resources need for
the object to function - basically, the method and the data. The object is said to
"publish its interfaces." Other objects adhere to these interfaces to use the object
without having to be concerned with how the object accomplishes it. The idea is
"don't tell me how you do it; just do it." An object can be thought of as a selfcontained
atom. The object interface consists of public methods and instantiate data.
Protection and information hiding are techniques used to accomplish encapsulation of an
object. Protection is when you limit the use of class data or methods.
More Concepts
Another growing concept in OOP is dynamic and static binding. Most languages provide
one or the other. C++ provides both. A method that is not virtual is said to be statically
bound, whereas virtual methods are said to be dynamically bound. Non-virtual methods
are statically bound, because the binding of the method is performed at compile and link
time and cannot be changed. Virtual methods are dynamically bound, because the binding
of the method is actually performed at run-time. When you call a virtual method, a small
lookup is performed in the object virtual table (a.k.a. vtable) to find the address of the
method being called. By manipulating an objects vtable at run-time,