Object Orriented Programming
Object Orriented Programming
Object-Oriented Programming
Introduction
Programming languages that support procedural and dataoriented, in addition to object-oriented programming. C++ and Ada95 are examples of such programming languages. Programming languages that were designed to support object-oriented programming and do not support other programming paradigms, but still employ some of the basic imperative structures. Java and C# are among such languages. Programming languages that offer complete support for object-oriented programming. Smalltalk is the only pure object-oriented language.
Object-Oriented Programming
The roots of the Object-Oriented programming concept refer to SIMULA67. However, the Object-Oriented concept was not fully developed until the evolution of Smalltalk 80. An Object-Oriented language must support three language features: abstract data types, inheritance, and dynamic binding
Inheritance
One of the most important opportunities for increasing software development productivity is the concept of software reuse. Software reuse is greatly facilitated with using inheritance, by which 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.
Inheritance Terminology
Class: the abstract data type in Object-Oriented languages. Object: A class instance. Derived class (subclass): A class that is defined through inheritance from another class. Parent class (superclass): A class from which a new class is derived. Methods: the subprograms that define the operations on objects of a class. Messages: the calls to methods. Messages have two parts--a method name and the destination objec. Message Protocol (Message Interface): the entire collection of an objects methods.
Inheritance Terminology
Classes can have two kinds of methods:
Instance Methods: operate only on the objects of the class Class Methods: can perform operations on the class, and possibly on the objects of the class
Instance Variables: every object of a class has its own set of instance variables, which store the objects state. Class Variables: belong to the class, so there is only one copy for the class.
data
methods
Masters Student
Student
Methods of Student
Inheritance Types
Single Inheritance: if a new class is a subclass of a single parent class. Relationships among classes in this type of inheritance can be shown in a derivation tree. Multiple Inheritance: if a new class has more then one parent class. Relationships among classes in this type of inheritance can be shown in a derivation graph.
Dynamic Binding
In order to understand the concept of dynamic binding, lets consider the following situation: there are two classes; a base class A and a subclass B. Class A defines a method and class B overrides that method. If a client of A and B has a reference or pointer to As objects. Then, that pointer or reference could also point at class B objects, making it a polymorphic reference or pointer. If the method is called through the polymorphic reference or pointer, then it is the role of the system to determine during execution which method should be called.
Shape
Triangle, Circle and Rectangle (subclass) Each of them compute the Area with different way
The Exclusivity of Objects Subclasses as Types Type Checking and Polymorphism Single and Multiple Inheritance Object Allocation and De-Allocation Dynamic and Static Binding Nested Classes
The exclusivity of objects refers to the exclusive use of objects in purest model of object-oriented computation, where all types are classes, and the classes are treated the same way, and all computation is performed through message passing. For instance, in Smalltalk, adding 7 to the variable x is done by sending object 7 to the + method of the object x.
The exclusivity of objects refers to the exclusive use of objects in purest model of object-oriented computation, where all types are classes, and the classes are treated the same way, and all computation is performed through message passing. For instance, in Smalltalk, adding 7 to the variable x is done by sending object 7 to the + method of the object x.
Everything is an object
Advantage - fast operations on simple objects Disadvantage - results in a confusing type system (two kinds of entities)
Include an imperative-style typing system for primitives but make everything else objects
Advantage - fast operations on simple objects and a relatively small typing system Disadvantage - still some confusion because of the two type systems
If an is-a relationship holds between a derived class and parent class, then this relationship guarantees that a variable of the derived class type could appear where a variable of the parent class type was legal, without causing a type error. Such a simple form of inheritance is apparent in Ada95 subtypes, as the following example clarifies: Subtype Small_Int is Integer range -100100 The above example means that every Small-Int is an integer. In addition, Small_Int have all the operations of Integer variables, but can only store a subset of the possible values in Integer.
The following are the two kinds of type checking that must be performed between a message and a method:
The messages parameter type must be checked against the methods formal parameters, and the methods return type needs to be checked against the expected return type of the message. An alternative is to delay type checking until the polymorphic variable is used to call a method. However, this alternative is expensive and delays type error detection.
The issue here is whether the language allows multiple inheritance in addition to single inheritance. The reasons for which the language designer does not include multiple inheritance even it is useful refer to the following: Complexity: several problems arise here:
When the parent classes both define identically named methods and one or both of them need to be overridden in the subclass. Diamond or Shared inheritance: this problem can be better illustrated with the following example: class C has both, A and B, as parent classes, where A and B are derived from the common class Z. The problem is: should C inherit which version of the variable inherited from Z?
Efficiency: the maintenance of systems that use multiple inheritance can be a serious problem because multiple inheritance leads to more complex dependencies among classes.
Two design questions arise regarding the allocation and deallocation of objects. These are:
The place from which objects are allocated. As it is known, objects could be allocated either from the run-time stack, or explicitly created on the heap with an operator or function, as new. Suppose that class B is a subclass of class A, and b1 the object of B needs to be assigned to an object a1 of A as: a1 = b1; then, if a1 and b1 are references to heap-dynamic objects, there is no problem. However, if a1 and b1 are stackdynamic, then the value of the object must be copied to the space of the target object. The problem arises when B adds a data field, then a1 will not have sufficient space for all of b1, resulting in a truncation for the excess in b1.
For objects allocated from the heap, the question that arises is whether deallocation is implicit, explicit, or both. If deallocation is implicit, then some implicit storage reclamation is required, such as reference counters or garbage collection. Explicit deallocation, on the other hand, raises the issue of whether dangling pointers or references can be created.
dynamic binding is an essential part of objectoriented programming. The issue is whether the user is allowed to specify if the binding is static or dynamic. The advantage obtained from this is that static binding is faster. So, if the binding needs not to be dynamic, then why to pay its price?
If a new class is needed by only one class, then it can be nested inside that class, which is called the nesting class. Two main issues appear here: Which of the nesting class facilities are visible in the nested class? And the opposite: Which of the nested class facilities are visible in the nesting class?
Smalltalk was the first language to include complete support for the object-oriented programming paradigm. General Characteristics
Objects: virtually everything, from a small integer constant to a complex file handing system, is an object. Messages: can be parameterized with variables that reference objects. Replies to messages have also the form of object. Allocation: all Smalltalk objects are allocated from the heap and are referenced through reference variables. Deallocation: all deallocation is implicit using a garbage collection
In Smalltalk, type checking is only dynamic, and the only type error occurs when a message is sent to an object that has no matching method. The goal of type checking in Smalltalk is to ensure that a message matches some method. Smalltalk variables are not typed (i.e., any name can be bound to any object). The meaning of an operation on a variable is determined by the variables class to which the variable is currently bound.
A Smalltalk subclass can inherit its superclass, instance variables, instance and class methods. A subclass can also have its own instance variables. The subclass can, in addition, define new methods and redefine methods of an ancestor class. If a subclass has a method with a name and protocol similar to that of the ancestor class, the subclass hides the ancestors method. In order to access the hidden method, the message is prefixed with the pseudovariable super. Smalltalk supports single inheritance only.
The syntax of Smalltalk is simple and very regular. Smalltalk is built around a simple but powerful concept, which is that all programming can be done using only a class hierarchy that uses objects, inheritance, and message passing. Compared to conventional complied imperative language programs, equivalent Smalltalk programs are slower. Dynamic binding in Smalltalk delays error detection until run time. Smalltalk user interface has had its impact on computing, by being the first to use integrated windows, mouse-pointing devices, and pop-up or pull-down menus. Smalltalk has the greatest impact on the advancement of objectoriented programming, which is the most common design and coding methodology.
C++ is a hybrid language: it supports both; procedural and object-oriented programming. C++ objects: can be static, stack-dynamic, or heap dynamic. Explicit deallocation using the delete operator is required for heap dynamic objects. C++ Constructor: at least, one constructor is used in a C++ class to initialize the new objects data members. When an object is created, constructor methods are implicitly called. If a constructor is not defined, the compiler includes the default constructor, which calls the parent class constructor. C++ Destructor: a destructor method is implicitly called when an object ceases to exit. The destructor main use is to delete heap-allocated data members. Also, it may be used to record part or all of the objects state for debugging purposes.
A C++ class can be derived from an existing class. C++ can also be, unlike Smalltalk, stand-alone without a parent class. The derived class can inherit some or all of the base class data members (data defined in a class) and member functions (functions defined in a class). In addition, the subclass can add new data members and member functions, and modify inherited data members and member functions.
The syntactic form of a derived class is as follows: Class derived_class_name : access_mode base_class_name {data member and member function declarations};
b , y ,c , z are private
Class c inherits all of the members of both A and B. If both A and B include the same name , they can be unambiguously .
When a polymorphic variable is used to call a function in a derived class, the call must be dynamically bound to the correct function definition. Therefore, member functions that must be dynamically bounded are preceded with the keyword virtual.
draw( )=0 used to indicate that this member function is pure virtual function : meaning that it has no body and cannot be called .
it must be redefined in derived classes. the purpose of a pure virtual function is to provide the interface of a function without giving any of its implementation . this allows every subclass to define its own version of the function .
any class include a pure virtual function is an abstract class. No object of an abstract class can be created .where the subclasses of such type can have objects.
Compared to Smalltalk:
C++ programmer has highly detailed control over members access. C++ allows multiple inheritance in addition to single inheritance. C++ programmer can decide whether to use static or dynamic binding. C++ provides static type checking. C++ provides generic classes through its template facility. C++ is a large and complex language. Efficiency is a strong argument in favor of C++.
Directly, Java supports only single inheritance. However, it uses a kind of abstract class, called interface, which serves as a version of multiple inheritance.
Directly, Java supports only single inheritance. However, it uses a kind of abstract class, called interface, which serves as a version of multiple inheritance. A method in Java can be defined to be final, so that it can not be overridden in any descendent class. If this keyword specifies a class definition, then the class can not be the parent of any subclass.
Java does not support procedural programming. Java does not allow parentless classes. Dynamic binding is the normal way used to bind method calls to method definitions. Java uses interface to support multiple inheritance.
Evaluation Since C# is based on its predecessors, from which designers learnt and improved, it is expected that some of the problems are remedied.
Even though JavaScript does not have classes and supports neither inheritance nor dynamic binding, it uses an object model that is based on C++ and Java objects
General Characteristics JavaScript is dynamically typed. JavaScript does not have classes. JavaScript cannot support class-based inheritance JavaScript cannot support polymorphism. JavaScript has two categories of variables; variables that directly store values of primitive types and variables that reference objects. Variables can be declared but no type is specified; the type changes every time the variable is assigned a value.
JavaScript is effective as a scripting language. However, its object model is not adequate for large software systems.
Summary
OO programming involves three fundamental concepts: ADTs, inheritance, dynamic binding Major design issues: exclusivity of objects, subclasses and subtypes, type checking and polymorphism, single and multiple inheritance, dynamic binding, explicit and implicit de-allocation of objects, and nested classes Smalltalk is a pure OOL C++ has two distinct type system (hybrid) Java is not a hybrid language like C++; it supports only OO programming C# is based on C++ and Java JavaScript is not an OOP language but provides interesting variations