+ All Categories

Oop

Date post: 08-Jul-2015
Category:
Upload: jun-jun-lagman
View: 263 times
Download: 2 times
Share this document with a friend
Description:
eto po yung reviewhin nyo mga idol sa 243 :D
Popular Tags:
18
OBJECT-ORIENTED PROGRAMMING
Transcript
Page 1: Oop

OBJECT-ORIENTED PROGRAMMING

Page 2: Oop

INTRODUCTION

Languages that support OOP are firmly entrenched in the mainstream.From COBOL to LISP c++, Ada 95, and CLOS, an OOP version of LISP. C++ and Ada 95 support procedural- and data-oriented programming, CLOS also supports funcitonal programming. Some of the newer languages that were designed to support object-oriented programming do not support other programming paradigms, but still employ some of the basic structures and have the appearance of the older imperative languages. Among these are Eiffel and Java. Smalltalk was the first language to offer complete support for object-oriented programming. This is inheritance, which is at the center of object-oriented programming and the languages that support it.

Page 3: Oop

Object-Oriented ProgrammingINTRODUCTIONINTRODUCTION

The concept of The concept of object-oriented programming object-oriented programming has its roots in SIMULA has its roots in SIMULA 67 but was not fully developed until the evolution of Smalltalk 67 but was not fully developed until the evolution of Smalltalk resulted in producing Smalltalk 80 (in1980, of course). resulted in producing Smalltalk 80 (in1980, of course). Three key languages:Three key languages:-abstract data types-abstract data types-inheritance-inheritance-a particular kind of dynamic binding-a particular kind of dynamic bindingProcedure-oriented programming focuses on subprograms and Procedure-oriented programming focuses on subprograms and subprogram libraries. Data-oriented programming focuses on subprogram libraries. Data-oriented programming focuses on abstract data types. The Sorting process is enacted by calling that abstract data types. The Sorting process is enacted by calling that operation on the specific array object. The data-oriented operation on the specific array object. The data-oriented programming paradigm was popular in the 1980s, and it served programming paradigm was popular in the 1980s, and it served by the data abstraction facilities of Modula-2, Ada, and several by the data abstraction facilities of Modula-2, Ada, and several more recent languages. more recent languages. Object-based Object-based languages are the languages languages are the languages that support data-oriented programming.that support data-oriented programming.

Page 4: Oop

INHERITANCEINHERITANCE

By the middle to late 1980s, it became apparent to many software By the middle to late 1980s, it became apparent to many software developers that one of the best opportunities for increased developers that one of the best opportunities for increased productivity in their profession was in software reuse. productivity in their profession was in software reuse. Abstract Abstract data types, with theirdata types, with their encapsulation and access controls,encapsulation and access controls, were obviously the units to be reused. The problem with the reuse were obviously the units to be reused. The problem with the reuse of abstract data types is that, nearly all cases, the features and of abstract data types is that, nearly all cases, the features and capabilities of the existing type are not quite right for the new use. capabilities of the existing type are not quite right for the new use. Second problem is with data-oriented programming is that all Second problem is with data-oriented programming is that all abstract data type definitions are independent and are at the same abstract data type definitions are independent and are at the same level. This often makes it impossible to structure a program to fit level. This often makes it impossible to structure a program to fit the problem space being addressed by the program.the problem space being addressed by the program.

Page 5: Oop

Inheritance offers a solution to both modification problem posedby abstract data type and the program organization problem. If a new abstract data type can inherit the data and functionality of some existing type, and is also allowed to modify some of those Entities and add new entities, reuse is greatly facilitated without changesto the reused data type.

The abstract data types in OOP, following the lead of SIMULA 67,are usually called classes. As with instances of abstract data types,Class instances are called objects. A class that is defined throughInheritance from another class is a derived class or subclass.A class from which the new class is derived is its parent class or superclass.The sub-programs that define the operations on objects of a class are Called methods. The calls to methods are often called messages. The entire collection of methods of an objects is called the message protocol, or message interface, of the object.

Page 6: Oop

A modified method has the same name. and often the same protocol,As the one of which it is a modification. The new methodis said to override the inherited version, which is then calledan overriden method. The most common purpose of an overriding methodIs to provide an operation that is specific for objects of the derived classbut is not appropriate for objects of the parent class.

classes can have two kinds of methods and variables.The most commonly used methods and variables are called instance methodsAnd variables. Every object of a class has its own set of instance variables,which store the objects state. The only difference between two objects ofthe same class is the state of their instance variables. Instance methodsoperate only on the objects of the class. Class variables belongto the class, rather than its object, so there only one copy for the class. Class methods can perform operations on the class, and possibly also on the objects of the class.

Page 7: Oop

If a new class is a subclass of a single parent class, then the Derivation process is called single inheritance. If a class has morethan one parent class, the process is called multiple inheritance.When a number of classes are related through single inheritance,their relationships to each other can be shown in a derivation tree. The class relationship in a multiple inheritance can be shown in a derivation graph. One disadvantage of inheritance as a meansOf increasing the possibility of reuse is that it creates a dependencyAmong the classes in an inheritance hierarchy.

Page 8: Oop

Polymorphism and Dynamic Binding

3rd characteristics of OOP language is a kind of polymorphism provided by the dynamic binding of messages to method definitions.This is supported by allowing one to define polymorphic variables of the type of the parent class that are alsoable to reference objects of any of the subclass of that class.The parent class can define a method that is overridden by its subclass.Virtual method, any class that includes at least one virtual method is called a virtual class. Such as class cannot beinstantiated because not all of its methods have bodies.

Page 9: Oop

Computing with an OOP Computing with an OOP languagelanguage

All computing in pure OOP language is done by the same uniform All computing in pure OOP language is done by the same uniform technique: sending a message to an object to invoke one of its technique: sending a message to an object to invoke one of its methods. A reply to a message is an object that returns the value methods. A reply to a message is an object that returns the value of the computation of the method.of the computation of the method.

An executing program in an object-oriented language can be An executing program in an object-oriented language can be described as a simulation of a collection of computers (objects) described as a simulation of a collection of computers (objects) that communicate with each other through messages.that communicate with each other through messages.

In addition, objects can send and receive messages. In essence, In addition, objects can send and receive messages. In essence, those are the fundamental capabilities of computers: to store and those are the fundamental capabilities of computers: to store and manipulate data and to communicate.manipulate data and to communicate.

The essence of object-oriented programming is solving problems The essence of object-oriented programming is solving problems by identifying the real-world objects of the problem and the by identifying the real-world objects of the problem and the processing required of those objects, and then creating processing required of those objects, and then creating simulations of those objects, their processes, and the required simulations of those objects, their processes, and the required communications between the objects.communications between the objects.

Page 10: Oop

Design Issues for Object-Design Issues for Object-Oriented LanguagesOriented Languages

The Exclusivity of ObjectsThe Exclusivity of Objects A language designer who is totally committed to the object model of A language designer who is totally committed to the object model of

computation designs an object system that absorbs all other concepts of computation designs an object system that absorbs all other concepts of type. The advantage of this choice is the elegant and pure uniformity of the type. The advantage of this choice is the elegant and pure uniformity of the language and its use. The primary disadvantage is that simple operations language and its use. The primary disadvantage is that simple operations must be done through the message-passing process which often makes must be done through the message-passing process which often makes them slower than similar operations in an imperative model, where them slower than similar operations in an imperative model, where machine instructions implement such simple operations. One alternative to machine instructions implement such simple operations. One alternative to the exclusive use of objects that is common in imperative languages to the exclusive use of objects that is common in imperative languages to which support for object-oriented programming has been added is to retain which support for object-oriented programming has been added is to retain a complete imperative typing model and simply add the object model. This a complete imperative typing model and simply add the object model. This result in a larger languages whose type structure is confusing to all but result in a larger languages whose type structure is confusing to all but expert users.expert users.

Another alternative to the exclusive use of objects is to have an Another alternative to the exclusive use of objects is to have an imperative-style type structure for the primitive scalar types. This provides imperative-style type structure for the primitive scalar types. This provides the speed of operations on primitive values that is comparable to those the speed of operations on primitive values that is comparable to those expected in the imperative model. Thisexpected in the imperative model. This alternative subclasses access to the alternative subclasses access to the “hidden” part of the parent class makes the subclass dependent on those “hidden” part of the parent class makes the subclass dependent on those details.details.

Page 11: Oop

On the other hand, keeping the implementation part of the parent class hidden from the subclasses can cause inefficiencies in the execution of the instances of those subclasses. This can be caused by the difference in efficiency of having direct access to data structures versus requiring access through the operations defined in the parent class. However, if the language designer has chosen interface inheritance, this code would look something like

int second ( ) { int temp = top ( ) ;

pop ( ) ; int temp_result = top ( ) ; push (temp) ; return temp_result;}

This is clearly a slower process than the direct access to the second element from the top of the stack. The best solution for the language designer is to provide both implementation and interface inheritance options to the software designer and let him or her decide, on a case-by-case basis, which version is better.

Page 12: Oop

Type Checking and PolymorphismType Checking and Polymorphism

Polymorphism in the object-oriented realm is defined to be the use of a Polymorphism in the object-oriented realm is defined to be the use of a polymorphic pointer or reference to access a method whose name is polymorphic pointer or reference to access a method whose name is overridden in the class hierarchy that defines the object to which the pointer overridden in the class hierarchy that defines the object to which the pointer or reference is defined to point.or reference is defined to point.

The Polymorphic Variable is the type of class, and the parent class defines at The Polymorphic Variable is the type of class, and the parent class defines at least the protocol of a method that is overridden by the derived classes. It can least the protocol of a method that is overridden by the derived classes. It can reference objects of the parent class and the derived classes, so the class of the reference objects of the parent class and the derived classes, so the class of the object to which it points cannot always be statically determined. The issue here object to which it points cannot always be statically determined. The issue here is when the type checking of this binding takes place. is when the type checking of this binding takes place.

This issue is an important one, for it aligns with the fundamental nature of the This issue is an important one, for it aligns with the fundamental nature of the programming language.programming language.

The use of Multiple Inheritance can easily lead to complex program The use of Multiple Inheritance can easily lead to complex program organizations. Maintenance of system that use multiple inheritance can be a organizations. Maintenance of system that use multiple inheritance can be a more serious problem, for multiple inheritance leads to more complex more serious problem, for multiple inheritance leads to more complex dependencies among classes. It is not clear to some that the benefits of multiple dependencies among classes. It is not clear to some that the benefits of multiple inheritance are worth the added effort to design and maintain a system using inheritance are worth the added effort to design and maintain a system using it.it.

Page 13: Oop

Allocation and Deallocation of Allocation and Deallocation of ObjectsObjects

There are two design questions concerning the allocation and There are two design questions concerning the allocation and deallocation of objects. deallocation of objects.

The first of these is the place from which objects are allocated. If The first of these is the place from which objects are allocated. If they behave like the abstract data types, then perhaps they can be they behave like the abstract data types, then perhaps they can be allocated from anywhere. This means they could be statically allocated from anywhere. This means they could be statically allocated by the compiler, allocated as stack-dynamic objects from allocated by the compiler, allocated as stack-dynamic objects from the run-time stack, or explicitly created on the heap with an the run-time stack, or explicitly created on the heap with an operator or function such as new.operator or function such as new.

The second question here is concerned with those cases where The second question here is concerned with those cases where objects are alloctated from the heap. The question is whether objects are alloctated from the heap. The question is whether deallocation is implicit or explicit or both. If deallocation is deallocation is implicit or explicit or both. If deallocation is implicit, some implicit method of storage reclamation is required, implicit, some implicit method of storage reclamation is required, such as reference counters or the garbage collection. If allocation such as reference counters or the garbage collection. If allocation can be explicit, that raises the issue of whether dangling pointers can be explicit, that raises the issue of whether dangling pointers or reference can be created.or reference can be created.

Page 14: Oop

Dynamic and Static BindingDynamic and Static Binding

The question here is whether all binding of The question here is whether all binding of messages to methods is dynamic. The messages to methods is dynamic. The alternative is to allow the user to specify alternative is to allow the user to specify whether a specific binding is to be whether a specific binding is to be dynamic or static. The advantage of this is dynamic or static. The advantage of this is that static bindings are faster. So if that static bindings are faster. So if binding need not be dynamic, why pay binding need not be dynamic, why pay price?price?

Page 15: Oop

Support for OOP in javaSupport for OOP in java As with c++, java does not use objects exclusively. However, in java only As with c++, java does not use objects exclusively. However, in java only

values of the primitive scalar types ( Boolean, character and the numeric) values of the primitive scalar types ( Boolean, character and the numeric) are not objects. Java has no enumeration or record types, and its arrays are not objects. Java has no enumeration or record types, and its arrays are objects. The reason to have nonobjects is efficiency. However, having a are objects. The reason to have nonobjects is efficiency. However, having a two type systems leads to some cumbersome situations. One of these in two type systems leads to some cumbersome situations. One of these in java is that the predefined data structure Vector can only contain objects. java is that the predefined data structure Vector can only contain objects. So if you want to put a primitive type value into a Vector object, the value So if you want to put a primitive type value into a Vector object, the value must first be placed in an object. This can be done by creating a new object must first be placed in an object. This can be done by creating a new object of the wrapper class for the primitive type. Such class has an instance of the wrapper class for the primitive type. Such class has an instance variable of that primitive type and a constructor that takes a value of the variable of that primitive type and a constructor that takes a value of the primitive type as a parameter and assigns it to its instance variable.primitive type as a parameter and assigns it to its instance variable.

Whereas c++ classes can be defined to have no parent, that is not possible Whereas c++ classes can be defined to have no parent, that is not possible in java. All java classes must be subclass of the root class, object, or some in java. All java classes must be subclass of the root class, object, or some class is that there are some operations that are universally needed. All java class is that there are some operations that are universally needed. All java objects are explicit heap dynamic. Most are allocated with the NEW objects are explicit heap dynamic. Most are allocated with the NEW operator.operator.

Page 16: Oop

InheritanceInheritance Java supports only single inheritance. However, it includes a kind of virtual Java supports only single inheritance. However, it includes a kind of virtual

class, called in interface, which provides aversion of multiple inheritance. class, called in interface, which provides aversion of multiple inheritance. An interface definition is similar to a class definition, except that it can An interface definition is similar to a class definition, except that it can contain named constants and method declaration. It defines only the contain named constants and method declaration. It defines only the specification of a class. The typical use of an interface is to define a class specification of a class. The typical use of an interface is to define a class that inherits both some of the methods and variables from its parent class, that inherits both some of the methods and variables from its parent class, and also implements a parent interface.and also implements a parent interface.

Applets are programs that are interpreted by a World Wide Web browser Applets are programs that are interpreted by a World Wide Web browser after being downloaded from a Web server. These applets all need certain after being downloaded from a Web server. These applets all need certain capabilities, which they can inherit from the predefined class, applet. This capabilities, which they can inherit from the predefined class, applet. This concurrency is supported by a predefined class named thread. But java concurrency is supported by a predefined class named thread. But java includes a predefined interface named runnable that supplies the interface includes a predefined interface named runnable that supplies the interface (but not the implementation) to one of the methods of Thread. The syntax (but not the implementation) to one of the methods of Thread. The syntax of the header of such an applet is exemplified by :of the header of such an applet is exemplified by :

public class Clock extends Applet implements Runnable.public class Clock extends Applet implements Runnable. In java, a method can be defined to be final, which means that it can not be In java, a method can be defined to be final, which means that it can not be

overriden in any descendant class. When the final reserved word is overriden in any descendant class. When the final reserved word is specified on a class definition, it means the class cannot be the parent of specified on a class definition, it means the class cannot be the parent of any subclass.any subclass.

Page 17: Oop

EncapsulationEncapsulation

2 Kinds of encapsulation :2 Kinds of encapsulation : Package- is a logical, rather than a physical, encapsulation: Any class Package- is a logical, rather than a physical, encapsulation: Any class

definition can specify that it belongs in a particular package.definition can specify that it belongs in a particular package. Classes- a class that does not specify a package name is placed in an Classes- a class that does not specify a package name is placed in an

unnamed package.unnamed package. A Package creates a new name space. Any method or variable that A Package creates a new name space. Any method or variable that

does not include an access modifier (private, protected, or public) has does not include an access modifier (private, protected, or public) has what is called Package Scope. This is an expansion of the definition of what is called Package Scope. This is an expansion of the definition of protected as used in C++, in which protected members are visible only protected as used in C++, in which protected members are visible only in the class where they are defined and in subclasses of that class. It is in the class where they are defined and in subclasses of that class. It is an alternative to the friends of C++, which are meant to provide access an alternative to the friends of C++, which are meant to provide access to private methods and instance variables in class to other specified to private methods and instance variables in class to other specified methods or classes. In effect, all non-private variables and methods of methods or classes. In effect, all non-private variables and methods of all the classes in a package areall the classes in a package are friends. friends.

Page 18: Oop

EvaluationEvaluation

Java’s design for supporting object-oriented Java’s design for supporting object-oriented programming is similar to that of C++, but differs programming is similar to that of C++, but differs from it in several significant ways. In keeping with from it in several significant ways. In keeping with its consistent adherence to object-oriented its consistent adherence to object-oriented principles, as in its lack of functions, Java does not principles, as in its lack of functions, Java does not allow parentless classes. It also uses dynamic allow parentless classes. It also uses dynamic binding as the “normal” approach to bind method binding as the “normal” approach to bind method calls to method definitions.calls to method definitions.


Recommended