Oop by edgar lagman jr

683 views
639 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
683
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Oop by edgar lagman jr

  1. 1. OBJECT-ORIENTED PROGRAMMING
  2. 2. 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.
  3. 3. Object-Oriented ProgrammingINTRODUCTIONINTRODUCTION The concept ofThe concept of object-oriented programmingobject-oriented programming has its roots in SIMULAhas its roots in SIMULA 67 but was not fully developed until the evolution of Smalltalk67 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 binding Procedure-oriented programming focuses on subprograms andProcedure-oriented programming focuses on subprograms and subprogram libraries. Data-oriented programming focuses onsubprogram libraries. Data-oriented programming focuses on abstract data types. The Sorting process is enacted by calling thatabstract data types. The Sorting process is enacted by calling that operation on the specific array object. The data-orientedoperation on the specific array object. The data-oriented programming paradigm was popular in the 1980s, and it servedprogramming paradigm was popular in the 1980s, and it served by the data abstraction facilities of Modula-2, Ada, and severalby the data abstraction facilities of Modula-2, Ada, and several more recent languages.more recent languages. Object-basedObject-based languages are the languageslanguages are the languages that support data-oriented programming.that support data-oriented programming.
  4. 4. INHERITANCEINHERITANCE  By the middle to late 1980s, it became apparent to many softwareBy the middle to late 1980s, it became apparent to many software developers that one of the best opportunities for increaseddevelopers that one of the best opportunities for increased productivity in their profession was in software reuse.productivity in their profession was in software reuse. AbstractAbstract 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 reusewere obviously the units to be reused. The problem with the reuse of abstract data types is that, nearly all cases, the features andof 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 allSecond problem is with data-oriented programming is that all abstract data type definitions are independent and are at the sameabstract data type definitions are independent and are at the same level. This often makes it impossible to structure a program to fitlevel. 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.
  5. 5. Inheritance offers a solution to both modification problem posed by 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 changes to 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 through Inheritance 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.
  6. 6. A modified method has the same name. and often the same protocol, As the one of which it is a modification. The new method is said to override the inherited version, which is then called an overriden method. The most common purpose of an overriding method Is to provide an operation that is specific for objects of the derived class but 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 methods And variables. Every object of a class has its own set of instance variables, which store the objects state. The only difference between two objects of the same class is the state of their instance variables. Instance methods operate only on the objects of the class. Class variables belong to 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.
  7. 7. If a new class is a subclass of a single parent class, then the Derivation process is called single inheritance. If a class has more than 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 means Of increasing the possibility of reuse is that it creates a dependency Among the classes in an inheritance hierarchy.
  8. 8. 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 also able 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 be instantiated because not all of its methods have bodies.
  9. 9. Computing with an OOPComputing with an OOP languagelanguage  All computing in pure OOP language is done by the same uniformAll computing in pure OOP language is done by the same uniform technique: sending a message to an object to invoke one of itstechnique: sending a message to an object to invoke one of its methods. A reply to a message is an object that returns the valuemethods. 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 beAn 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 andthose 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 problemsThe essence of object-oriented programming is solving problems by identifying the real-world objects of the problem and theby identifying the real-world objects of the problem and the processing required of those objects, and then creatingprocessing required of those objects, and then creating simulations of those objects, their processes, and the requiredsimulations of those objects, their processes, and the required communications between the objects.communications between the objects.
  10. 10. 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 ofA language designer who is totally committed to the object model of computation designs an object system that absorbs all other concepts ofcomputation designs an object system that absorbs all other concepts of type. The advantage of this choice is the elegant and pure uniformity of thetype. The advantage of this choice is the elegant and pure uniformity of the language and its use. The primary disadvantage is that simple operationslanguage and its use. The primary disadvantage is that simple operations must be done through the message-passing process which often makesmust be done through the message-passing process which often makes them slower than similar operations in an imperative model, wherethem slower than similar operations in an imperative model, where machine instructions implement such simple operations. One alternative tomachine instructions implement such simple operations. One alternative to the exclusive use of objects that is common in imperative languages tothe exclusive use of objects that is common in imperative languages to which support for object-oriented programming has been added is to retainwhich support for object-oriented programming has been added is to retain a complete imperative typing model and simply add the object model. Thisa complete imperative typing model and simply add the object model. This result in a larger languages whose type structure is confusing to all butresult 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 anAnother alternative to the exclusive use of objects is to have an imperative-style type structure for the primitive scalar types. This providesimperative-style type structure for the primitive scalar types. This provides the speed of operations on primitive values that is comparable to thosethe 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 thealternative 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.
  11. 11. 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.
  12. 12. Type Checking and PolymorphismType Checking and Polymorphism  Polymorphism in the object-oriented realm is defined to be the use of aPolymorphism in the object-oriented realm is defined to be the use of a polymorphic pointer or reference to access a method whose name ispolymorphic pointer or reference to access a method whose name is overridden in the class hierarchy that defines the object to which the pointeroverridden 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 atThe 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 canleast 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 thereference 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 hereobject 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 theThis 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 programThe use of Multiple Inheritance can easily lead to complex program organizations. Maintenance of system that use multiple inheritance can be aorganizations. Maintenance of system that use multiple inheritance can be a more serious problem, for multiple inheritance leads to more complexmore serious problem, for multiple inheritance leads to more complex dependencies among classes. It is not clear to some that the benefits of multipledependencies 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 usinginheritance are worth the added effort to design and maintain a system using it.it.
  13. 13. Allocation and Deallocation ofAllocation and Deallocation of ObjectsObjects  There are two design questions concerning the allocation andThere 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. IfThe first of these is the place from which objects are allocated. If they behave like the abstract data types, then perhaps they can bethey behave like the abstract data types, then perhaps they can be allocated from anywhere. This means they could be staticallyallocated from anywhere. This means they could be statically allocated by the compiler, allocated as stack-dynamic objects fromallocated by the compiler, allocated as stack-dynamic objects from the run-time stack, or explicitly created on the heap with anthe 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 whereThe second question here is concerned with those cases where objects are alloctated from the heap. The question is whetherobjects are alloctated from the heap. The question is whether deallocation is implicit or explicit or both. If deallocation isdeallocation 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 allocationsuch as reference counters or the garbage collection. If allocation can be explicit, that raises the issue of whether dangling pointerscan be explicit, that raises the issue of whether dangling pointers or reference can be created.or reference can be created.
  14. 14. Dynamic and Static BindingDynamic and Static Binding  The question here is whether all binding ofThe question here is whether all binding of messages to methods is dynamic. Themessages to methods is dynamic. The alternative is to allow the user to specifyalternative is to allow the user to specify whether a specific binding is to bewhether a specific binding is to be dynamic or static. The advantage of this isdynamic or static. The advantage of this is that static bindings are faster. So ifthat static bindings are faster. So if binding need not be dynamic, why paybinding need not be dynamic, why pay price?price?
  15. 15. Support for OOP in javaSupport for OOP in java  As with c++, java does not use objects exclusively. However, in java onlyAs 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 arraysare not objects. Java has no enumeration or record types, and its arrays are objects. The reason to have nonobjects is efficiency. However, having aare objects. The reason to have nonobjects is efficiency. However, having a two type systems leads to some cumbersome situations. One of these intwo 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 valueSo 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 objectmust 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 instanceof 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 thevariable 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 possibleWhereas 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 somein 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 javaclass is that there are some operations that are universally needed. All java objects are explicit heap dynamic. Most are allocated with the NEWobjects are explicit heap dynamic. Most are allocated with the NEW operator.operator.
  16. 16. InheritanceInheritance  Java supports only single inheritance. However, it includes a kind of virtualJava 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 canAn interface definition is similar to a class definition, except that it can contain named constants and method declaration. It defines only thecontain named constants and method declaration. It defines only the specification of a class. The typical use of an interface is to define a classspecification 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 browserApplets are programs that are interpreted by a World Wide Web browser after being downloaded from a Web server. These applets all need certainafter being downloaded from a Web server. These applets all need certain capabilities, which they can inherit from the predefined class, applet. Thiscapabilities, which they can inherit from the predefined class, applet. This concurrency is supported by a predefined class named thread. But javaconcurrency is supported by a predefined class named thread. But java includes a predefined interface named runnable that supplies the interfaceincludes 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 beIn 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 isoverriden in any descendant class. When the final reserved word is specified on a class definition, it means the class cannot be the parent ofspecified on a class definition, it means the class cannot be the parent of any subclass.any subclass.
  17. 17. EncapsulationEncapsulation 2 Kinds of encapsulation :2 Kinds of encapsulation :  Package- is a logical, rather than a physical, encapsulation: Any classPackage- 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 anClasses- 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 thatA Package creates a new name space. Any method or variable that does not include an access modifier (private, protected, or public) hasdoes not include an access modifier (private, protected, or public) has what is called Package Scope. This is an expansion of the definition ofwhat is called Package Scope. This is an expansion of the definition of protected as used in C++, in which protected members are visible onlyprotected 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 isin 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 accessan alternative to the friends of C++, which are meant to provide access to private methods and instance variables in class to other specifiedto private methods and instance variables in class to other specified methods or classes. In effect, all non-private variables and methods ofmethods 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.
  18. 18. EvaluationEvaluation  Java’s design for supporting object-orientedJava’s design for supporting object-oriented programming is similar to that of C++, but differsprogramming is similar to that of C++, but differs from it in several significant ways. In keeping withfrom it in several significant ways. In keeping with its consistent adherence to object-orientedits consistent adherence to object-oriented principles, as in its lack of functions, Java does notprinciples, as in its lack of functions, Java does not allow parentless classes. It also uses dynamicallow parentless classes. It also uses dynamic binding as the “normal” approach to bind methodbinding as the “normal” approach to bind method calls to method definitions.calls to method definitions.

×