Sei sulla pagina 1di 4

OOP Objects

In the world of software and programs we can not touch and hold the objects and sometimes we can not see them. When working on your computer you encounter software objects. Windows, menus, command buttons, input forms, edit fields, and hyperlinks are examples of the objects we are talking about. We also refer to certain objects as controls. These are objects contained within other objects. A window object can have different controls such as buttons, pictures, database links and check boxes to mention a few. Then we have a type of object that we cannot see. These are non-visual objects. A database connection, a user-defined function, an error handler these could be candidates for non-visual objects Software objects also have properties. Think of a command button it has a certain size, a face color, a name, some text Now real world objects can often do certain things. They are designed for a specific purpose and function. The same applies to software objects. They can do things and we refer to these things as methods. A window can open and close. To open a window named w_post_entries we will call the open method as follows:
open (w_post_entries)

To close this window we call the close method:


close (w_post_entries)

Object Properties and Methods


Object Properties Properties are the attributes that define the appearance of objects. Some properties of the window object are:
BackColor Border Center Enabled Resizeable Title Visible WindowType

Properties can be set at design time and can be changed dynamically with code at runtime. Object Methods Some methods of the window object are:
Activate() ArrangeSheets() Clicked() Close() Hide() Open() Resize()

Methods determine the behaviour of an object. Most methods are pre-defined and pre-coded. There are two categories of methods: functions and events. Functions A function is a program that performs specific processing. Repetitive tasks are good candidates for a function. Functions are also used to act on obects and controls. There are many built in functions. The following three functions are example of built-in functions.
Hide() Show() SetFocus()

Functions perform specific repetitve tasks, manipulate or change the object, or provide information about the object. Functions can also trigger events. So for example the open() function will trigger the open() event. In addition to built-in functions you can write your own user-defined functions. Events An event occurs as a result of user and system actions. When a button is clicked with the mouse, the buttons clicked event is triggered. There are many pre-defined events. Here are a few examples of events for a window object
Activate() Close() Open()

The events are actions in the window; when an event is triggered, the associated script is executed.

OOP Core Principles


The three core principles of Object Oriented Programming (OOP) are
Encapsulation Inheritance Polymorphism

ENCAPSULATION Assume we have an object and the object has the ability to change color. The object should provide a method that changes its color. Lets say this methods name is SetColor and it requires a parameter (the color) to be passed to it. Access to this method should be via an interface. This means in order to change the color you will call the setcolor() method.
Object.SetColor(blue)

When calling the setcolor() method it is not important for the user to know the internal workings and variable values of the method. It is only important to know which method to call and which parameters if any have to be passed to the method. This is the interface. How the method actually changes the color is the implementation. Think of a camera. It has a zoom button. This button is the interface that allows you to zoom the lens in or out. The exact mechanics of this process is not important. The camera is encapsulated, providing you with an interface which triggers the zoom function. A well encapsulated object has a public interface that is independent of the internal implementation. INHERITANCE AND CLASSES Objects can be grouped into classes. A class is a detailed description of an object. It is the blueprint for an object. Think of an ideal car that will form the template for other cars. Other cars derived from this template are sub-classes of the original and as such they will share all or some of the characteristics of the original. In relation to the sub-classes, the original class is the super class. In the world of OOP we refer to this process of deriving sub-classes as inheritance.

Lets consider two windows created from the same blueprint. In the beginning they both have the same properties and methods. Now from here the programmer can customize each window to suit the needs of the application. We know a window is an object. And so is a button. All windows are basically the same, as are all buttons. By having one master set of programs that create and define objects, the objects can be used again and again without having to re-write the code. In OOP the window object already exists we do not have to write the code that creates the window. In your development environment, whether it be Powerbuilder or some other package, the base or super classes already exist. These packages come with windows, buttons, edit fields and other pre-developed objects and classes. So a window is derived (inherited) from the window class and a button is derived from the button class. The attributes and methods of the ancestor class are inherited by the descendant object. Inheritance offers the following benefits:
Re-usability Consistency Less redundancy

When we use an object in our program we are creating an instance of a class. An object is also referred to as an instance of a class. POLYMORPHISM Polymorphism literally means many forms. For our purposes lets think of a function called draw(). It would be useful if draw could create different shapes depending on the parameters passed.
Draw(square) Draw(circle) Draw(polygon)

Functions with the same name but with different parameter lists (signatures) are examples of polymorphism. Such functions are also known as overloaded functions.

Potrebbero piacerti anche