OOP

The History of Object Oriented Programming

The basis for OOP started in the early 1960s. A breakthrough involving instances and objects was achieved at MIT with the PDP-1, and the first programming language to use objects was Simula 67. It was designed for the purpose of creating simulations, and was developed by Kristen Nygaard and Ole-Johan Dahl in Norway.

They were working on simulations that deal with exploding ships, and realized they could group the ships into different categories. Each ship type would have its own class, and the class would generate its unique behavior and data. Simula was not only responsible for introducing the concept of a class, but it also introduced the instance of a class.

The term "object oriented programming " was first used by Xerox PARC in their Smalltalk programming language. The term was used to refer to the process of using objects as the foundation for computation. The Smalltalk team was inspired by the Simula 67 project, but they designed Smalltalk so that it would be dynamic. The objects could be changed, created, or deleted, and this was different from the static systems that were commonly used. Smalltalk was also the first programming language to introduce the inheritance concept. It is this feature that allowed Smalltalk to surpass both Simula 67 and the analog programming systems. While these systems were advanced for their time, they did not use the inheritance concept.

Simula 67 was a groundbreaking system that has inspired a large number of other programming languages, and some of these include Pascal and Lisp. By the 1980s, object oriented programming had become prominent, and the primary factor in this is C++. Object oriented programming was also important for the development of Graphical user interfaces. The Cocoa structure that exists within Mac OS X is a good example of a dynamic GUI that works with an object oriented programming language. This paradigm of programming has also played an important role in the development of event- driven programming.

Niklaus Wirth and his associates were looking at areas such as modular programming and data abstraction, and they developed two systems which incorporated these elements. These two systems are Oberon and Modula-2. Oberon used a unique approach to classes and object orientation that is much different than C++ or Smalltalk. Since the introduction of OOP, a large number of modern programming languages are now using the concept. Some of these are Fortran, BASIC, and Pascal. There have been some compatibility issues, because many programs were not designed with a OOPs approach in mind. Object oriented programming languages that were "pure" did not have many of the functions that programmers needed.

To solve these problems, a number of researchers have been attempting to design new programming languages that used object oriented concepts but still retained many of the functions that programmers needed. One example of a programming language that has achieved this to some degree is Eiffel. Another programming language that has attempted to solve this problem is Java. Java has become popular because it uses a virtual machine, and it is very similar to C++ and C. The virtual machine is important because it allows code to be run on multiple platforms without having to be changed. Another system that is similar is Microsoft's .NET. Many developers now understand the importance of OOP, and are actively using it within their own programs. Many researchers have continued to make advancements by using the object oriented approach.

There are a number of other languages that have successfully combined the object oriented approach with procedures that are useful to programmers. Python is one example, and Ruby uses a similar approach as well.

The use of an object oriented approach has led to advancements in modeling languages, design patterns, and a number of other areas. It is likely that OOP is a programming paradigm that will continue to evolve as we move forward into the future. It is a powerful language which has continued to improve over the years. It is the subject of debate within the programming community, as critics point out a number of problems with the structure. However, the popularity of programming languages such as Java demonstrate that it is a paradigm that is here to stay.




Four major principles of Object-Oriented Programming (OOP)

The Objects Oriented Programming (OOP) is constructed over four major principles: Encapsulation, Data Abstraction, Polymorphism and Inheritance.

1. Encapsulation:
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition. Typically, only the object’s own methods can directly inspect or manipulate its fields.

Encapsulation is the hiding of data implementation by restricting access to accessors and mutators.

An accessor is a method that is used to ask an object about itself. In OOP, these are usually in the form of properties, which have a get method, which is an accessor method. However, accessor methods are not restricted to properties and can be any public method that gives information about the state of the object. A Mutator is public method that is used to modify the state of an object, while hiding the implementation of exactly how the data gets modified. It’s the set method that lets the caller modify the member data behind the scenes.

Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. This type of data protection and implementation protection is called Encapsulation.

A benefit of encapsulation is that it can reduce system complexity.

2. Abstraction
Data abstraction and encapuslation are closely tied together, because a simple definition of data abstraction is the development of classes, objects, types in terms of their interfaces and functionality, instead of their implementation details. Abstraction denotes a model, a view, or some other focused representation for an actual item.

“An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.” — G. Booch

In short, data abstraction is nothing more than the implementation of an object that contains the same essential properties and actions we can find in the original object we are representing.

3. Inheritance
Inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, parent classes or ancestor classes. The resulting classes are known as derived classes, subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy.

Subclasses and Superclasses A subclass is a modular, derivative class that inherits one or more properties from another class (called the superclass). The properties commonly include class data variables, properties, and methods or functions. The superclass establishes a common interface and foundational functionality, which specialized subclasses can inherit, modify, and supplement. The software inherited by a subclass is considered reused in the subclass. In some cases, a subclass may customize or redefine a method inherited from the superclass. A superclass method which can be redefined in this way is called a virtual method.

4. Polymorphism
Polymorphism means one name, many forms. Polymorphism manifests itself by having multiple methods all with the same name, but slightly different functionality.

There are 2 basic types of polymorphism. Overridding, also called run-time polymorphism. For method overloading, the compiler determines which method will be executed, and this decision is made when the code gets compiled.

Overloading, which is referred to as compile-time polymorphism. Method will be used for method overriding is determined at runtime based on the dynamic type of an object.




Advantages of an Object-Oriented Approach (for new programmers)
If you are new to OOP or to programming in general, I’ll give you a very simplified and quick assessment of approaching a new software design with object-oriented eyes.

Like structured programming in legacy systems, object-oriented programming (OOP) is used to manage the complexity of software systems. However, OOP technology provides several advantages. OOP applications are easier to maintain, have more reusable components, and are more scalable, to name a few.

Maintainable
OOP methods make code more maintainable. Identifying the source of errors becomes easier because objects are self-contained (encapsulation). The principles of good OOP design contribute to an application’s maintainability.

Reusable
Because objects contain both data and functions that act on data, objects can be thought of as self-contained “boxes” (encapsulation). This feature makes it easy to reuse code in new systems. Messages provide a predefined interface to an object’s data and functionality. If you know this interface, you can make use on an object in any context you want. OOP languages, such as C# and VB.Net, make it easy to expand on the functionality of these “boxes” (polymorphism and inheritance), even if you don’t know much about their implementation (again, encapsulation).

Scalable
OO applications are more scalable then their structured programming roots. As an object’s interface provides a roadmap for reusing the object in new software, it also provides you with all the information you need to replace the object without affecting other code. This makes it easy to replace old and aging code with faster algorithms and newer technology.

Some Disadvantages
The challenges of OOP exists mainly in the conversion of legacy systems that are built in structured programming languages. The technical challenge is not as big as the actual design challenge. The goal when converting is to minimize the effects of the stuctural systems on the OO nature of the new design, and this can sometimes be difficult.




Object-Oriented JavaScript

In this chapter, you'll learn about OOP (Object-Oriented Programming) and how it relates to JavaScript. As an ASP.NET developer, you probably have some experience working with objects, and you may even be familiar with concepts such as inheritance. However, unless you're already an experienced JavaScript programmer, you probably aren't familiar with the way JavaScript objects and functions really work. This knowledge is necessary in order to understand how the Microsoft AJAX Library works, and this chapter will teach you the necessary foundations. More specifically, you will learn:

What encapsulation, inheritance, and polymorphism
mean
How JavaScript functions work
How to use anonymous functions and closures
How to read a class diagram, and implement it using
JavaScript code
How to work with JavaScript prototypes
How the execution context and scope affect the output of
JavaScript functions
How to implement inheritance using closures and
prototypes
What JSON is, and what a JSON structure looks like
In the next chapters you'll use this theory to work effectively with the Microsoft AJAX Library.
Concepts of Object-Oriented Programming

Most ASP.NET developers are familiar with the fundamental OOP principles because this knowledge is important when developing for the .NET development. Similarly, to develop client-side code using the Microsoft AJAX Library, you need to be familiar with JavaScript's OOP features. Although not particularly difficult, understanding these features can be a bit challenging at first, because JavaScript's OOP model is different than that of languages such as C#, VB.NET, C++, or Java.

JavaScript is an object-based language. Just as in C#, you can create objects, call their methods, pass them as parameters, and so on. You could see this clearly when working with the DOM, where you manipulated the HTML document through the methods and properties of the implicit documentobject. However, JavaScript isn't generally considered a fully object- oriented language because it lacks support for some features that you'd find in "real" OOP languages, or simply implements them differently.

Your most important goal for this chapter is to understand how to work with JavaScript objects. As an ASP.NET developer, we assume that you already know how OOP works with .NET languages, although advanced knowledge isn't necessary. A tutorial written by Cristian Darie on OOP development with C# can be downloaded in PDF format.

To ensure we start off from the same square, in the following couple of pages we'll review the essential OOP concepts as they apply in C# and other languages—objects, classes, encapsulation, inheritance, and polymorphism. Then we'll continue by "porting" this knowledge into the JavaScript realm.

Objects and Classes

What does "object-oriented programming" mean anyway? Basically, as the name suggests, OOP puts objects at the cent of the programming model. The object is probably the most important concept in the world of OOP—a self-contained entity that has state and behavior, just like a real-world object. Each object is an instance of a class (also called type),
which defines the behavior that is shared by all its objects.

We often use objects and classes in our programs to represent real-world objects, and types (classes) of objects. For example, we can have classes like Car, Customer, Document, or Person, and objects such as myCar, johnsCar, or davesCar.

The concept is intuitive: the class represents the blueprint, or model, and objects are particular instances of that model. For example, all objects of type Car will have the same behavior—for example, the ability to change gear. However, each individual Car object may be in a different gear at any particular time—each object has its particular state. In programming, an object's state is described by its fields and properties, and its behavior is defined by its methods and events.

You've already worked with objects in the previous chapter. First, you've worked with the built-in documentobject. This is a default DOM object that represents the current page, and it allows you to alter the state of the page. However, you also learned how to create your own objects, when you created the xmlHttp object. In that case, xmlHttp is an object of the XMLHttpRequestclass. You could create more XMLHttpRequest objects, and all of them would have the same abilities (behavior), such as the ability to contact remote servers as you learned earlier, but each would have a different state. For example, each of them may be contacting a different server.

In OOP's world everything revolves around objects and classes, and OOP languages usually offer three specific features for manipulating them—encapsulation, inheritance, and polymorphism.




Understanding The Message Concept In OOPs

To understand object oriented programming, you will need to become familiar with messages. As the name implies, a message is a process in which software objects will communicate with one another. Because of this, having one object is not enough.

An object will work best when it is exists in a large application that is populated by other objects. When these objects interact with each other, programmers will be able to gain a high level of functionality, and the behavior of the system will be very complex. To give you an example of this, imagine that you have a motorcycle that is sitting in your garage. By itself, it is nothing but gears and metal. However, when you sit on it, turn it on, and begin riding it, both you and the motorcycle have created interaction.

The software objects that exist within a program must interact as well. They will do this by sending messages to each other. When object Z wants object R to initiate one of R's methods, object Z will transmit a signal to object R. There is a possibility that object R will not have enough information to activate the method. For example, if you want your motorcycle to slow down, you will need to "communicate" with it by pressing down on the brakes. The data that is transferred with the message is called parameters. The messages that are sent between software objects are comprised of three things. The first is the object to which the message is being sent. The second is the name of the method that is being requested. The third is the parameters that must be used with the method.

The messages use parameters to make sure the information is precise. If the receiving object does not have enough information, it will not be able to properly carry out the method. The three parts that make up a message are enough for the receiving software object to carry out the requested method. There is no other information that is needed. There are two important advantages that messages have. First, the behavior of an object will be displayed through the methods it uses. Because of this, transferring messages will allow a large number of interactions to occur between objects. Second, the objects will not have to be in the same machine in order to send or receive messages. It is also not necessary for them to be in the same process.

Encapsulation plays an important role in object oriented programming. It can be defined as concealing decisions which are related to the design of a computer program. The information that will often be hidden are things that are likely to be altered. The purpose of encapsulation is to protect the other parts of the application from changing when one piece of information is changed. To conceal a design decision, a programmer will need to build a strong interface. This interface will cover the rest of the application from any changes that are made. In contemporary programming languages, the concept of encapsulation has been used in a number of different ways.

While some people choose to use encapsulation as a general term for hiding information, others use it in a manner which is much more specific. In object oriented programming, the software module may hide the data by encapsulating it within a module which showcases an interface. It will reduce the chances of a software being damaged during its development by transferring the dependency of the code into an interface which is secure. The elements will carry out operations on the interface so that if a change occurs, it is not necessary for the clients to change as well. In most cases, the information will be hidden by concealing the layout of the data. If the layout is altered, the change will be reduced to a small portion of the program.

Information hiding is important for splitting up software, hardware, or equipment into parts that are functional. A good example of this is a car. A car is a intricate device that is split into a number of modules. Each module will have an interface which hides the decisions which are related to design. This will allow car companies to offer a number of products while maintaining a car which is cost effective to build.




Walang komento:

Mag-post ng isang Komento