Principles of Object-Oriented Programming

Go down

Principles of Object-Oriented Programming

Post by Admin on Mon Aug 13, 2018 6:48 pm

Principles of Object-Oriented Programming


Object-oriented programming (OOP)
is a programming model that focuses on anapplication’s components and data and the methods you need to manipulate them. With OOP, you consider the items
that a program will manipulate—for example, a customer invoice, a loan application, a button that a
user clicks, or a menu from which a user selects an option. These items are called objects, and when
you program, you define their characteristics, functions, and capabilities.


Five important features of object-oriented languages are:

- Classes
- Objects
- Polymorphism
- Inheritance
- Encapsulation


Classes and Objects

In object-oriented terminology, a class describes a group or collection of objects with
common attributes. An object is one instance of a class. Object-oriented programmers
sometimes say an object is one instantiation of a class; when a program creates an object,
it instantiates the object.




Objects both in the real world and in object-oriented programming may contain attributes
and methods. Attributes are the characteristics of an object. For example, some of your
automobile’s attributes are its make, model, year, and purchase price. These attributes don’t
change during the car’s life. Examples of attributes that change frequently include whether
the automobile is currently running, its gear, its speed, and whether it is dirty. All automobiles
possess the same attributes, but not the same values for those attributes. Similarly, your dog
has the attributes of its breed, name, age, and whether its shots are current. Methods are the
actions that can be taken on an object; often they alter, use, or retrieve the attributes. For
example, an automobile has methods for changing and viewing its speed, and a dog has
methods for setting and finding out its shot status.


Polymorphism

Polymorphism
is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.

The real world is full of objects. Consider a door. A door needs to be opened and closed. You
open a door with an easy-to-use interface known as a doorknob. Object-oriented
programmers would say you are passing a message to the door when you tell it to open by
turning its knob. The same message (turning a knob) has a different result when applied to
your radio than when applied to a door. As depicted in Figure 10-2, the procedure you use to
open something—call it the “open” procedure—works differently on a door than it does on
a desk drawer, a bank account, a computer file, or your eyes. However, even though
these procedures operate differently using the various objects, you can call each of these
procedures “open.



Within classes in object-oriented programs, you can create multiple methods with the same
name, which will act differently and appropriately when used with different types of objects.


Inheritance


It is the process of acquiring the traits of one’s predecessors. In the real world, a new door
with a stained glass window inherits most of its traits from a standard door. It has the same
purpose, it opens and closes in the same way, and it has the same knob and hinges.

With object-oriented programming, once you create an object, you can develop new objects
that possess all the traits of the original object plus any new traits you desire. If you develop
a CustomerBill class of objects, there is no need to develop an OverdueCustomerBill class from scratch. You can create the new class to contain all the characteristics of the already
developed one, and simply add necessary new characteristics. This not only reduces the work
involved in creating new objects, it makes them easier to understand because they possess
most of the characteristics of already developed objects.




Encapsulation

Real-world objects often employ encapsulation and information hiding. Encapsulation is the
process of combining all of an object’s attributes and methods into a single package; the
package includes data that is frequently hidden from outside classes as well as methods that
are available to outside classes to access and alter the data. Information hiding is the concept
that other classes should not alter an object’s attributes—only the methods of an object’s own
class should have that privilege. (The concept is also called data hiding .) Outside classes
should only be allowed to make a request that an attribute be altered; then it is up to the
class’s methods to determine whether the request is appropriate.



In summary, understanding object-oriented programming means that you must consider five
of its integral components: classes, objects, polymorphism, inheritance, and encapsulation.








avatar
Admin
Admin

Posts : 3
Join date : 2018-08-13

View user profile http://maamlara.forumotion.asia

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum