CSC/ECE 517 Fall 2009/wiki2 15 sm

From Expertiza_Wiki
Jump to navigation Jump to search

Abstraction and the Object Model

Introduction

Abstraction is a process in which some characteristics of an entity are removed to reduce it to a set of essential characteristics that can effectively define that entity. It is basically done to reduce complexity. For example : the abstraction of a plastic container to a container will retain only the general information on the behavior and attributes of the container. Similarly, to use a television, we don't need to know about its internal complexity. This principle of abstraction is an essential element of Object-Oriented programming. In object-oriented software, abstraction is a way to managing complexity. A well designed abstraction is simple and slanted towards the user perspective. One of the most powerful ways of managing abstraction is by using hierarchical classifications. This helps in layering the semantics of complex systems thus breaking them into chunks of manageable pieces. This method of hierarchical abstractions can also be applied to computer programs. The data from a process-oriented program can be transformed into its component objects using the principle of abstraction. In programming languages, abstraction is a mechanism that emphasizes the general properties of some segment of code and hides details. It involves separating a program into parts that contains certain details and parts where these details are hidden. In Object-Oriented terminology, data is considered as attributes and the functions are referred to as methods.

The main advantage of Object-Oriented programming is that the data as well as the operations that manipulate the data known as code, are both encapsulated inside the object. An object is a collection of variables and related methods. A method is an operation that is used to modify the behavior of an object by manipulating its variables. For example : a Java Applet is an object. The browser executing that particular object has no idea about its functionalities. When the object is loaded, the code inside it is executed by the browser using the data contained within that particular object. Objects are the building blocks of Object Oriented programming. The state of an object is the data contained inside that object also referred to as attributes. These attributes help in differentiating between the various objects. In object-oriented programming, the methods define the behavior of an object. The concept of getters and setters sometimes referred to as accessor methods provide controlled access to an object's data.

A class is a kind of template from which objects are made. For example : if we are creating two employees, it is said that we have created two instances of the employee class with each instance or object having its own attributes and methods. An object can be instantiated or built only by using a class.

For Example : To instantiate an object in java :

                                     myClass myObject;

Here myClass is a class and myObject is an object.



Overview

In this article, we will try to find out what abstraction is all about, different types of abstraction that are used in Object-Oriented programming. We will also try to explore the early history of object-oriented programming. How the concept of abstraction slowly gained ground. We will be looking at some of the key features of abstraction in ealy languages. Then we will look deeply into the creation and implementation of abstraction in modern programming languages like Java and C++.



A Basic Idea

The central idea behind object-oriented programming is the concept of objects. Object-oriented programming approach helps the programmer to create modules which enable them to reuse that module when a new object is added. It just needs the creation of a new object that inherits the properties of the existing objects. Abstraction refers to the elimination of the irrelevant details and the amplification of the essential aspects in programming. It provides a basic platform for the creation of user defined data types which we call as objects. The mechanism by which abstraction can be implemented is encapsulation.

Abstract classes are declared with the keyword abstract and they cannot be instantiated. They can only be used as a superclass for classes that extend the abstract class. A class can inherit from only one abstract class and has to override all its methods and, if it wants, can override its virtual methods too. There are two common terms associated with the term abstraction:

  • Client - that part of the program that uses the program component.
  • Implementation - That part of the program that defines the program component.

The interaction between these two entities is usually restricted to a specific interface. Most of the modern object-oriented languages provide access to only a set of public operations on an object. This restriction is provided by the designer and the implementer of the object. For Example : there is a program that manipulates geometric shapes. In such a program, each shape can be represented as an object. An object representing a circle can be implemented by storing the center and radius of the circle. The designer of circle object can choose if the function that changes the center of the circle can be made part of the interface or not. Abstraction that is based on objects is quite similar to the abstraction based on abstract data types both combining functions and data as well as distinguishing between a public and a private interface. Abstraction and encapsulation are two aspects of Object-Oriented programming that go together. The type defined by a class is partially abstract as it can have both public and hidden components. When the hidden part is empty, the class resembles an object-oriented interface. This kind of classes is sometimes called as abstract class.



Types of Abstraction

There are two kinds of abstraction:

  • Procedural Abstraction - This is one of the oldest abstraction mechanisms. The program making a function call is considered as a client and the function body consisting of instructions executed each time the function is called is considered as the implementation.

For Example :

A code that stores the square root of a variable x in the variable y can be encapsulated into a function. By doing this, an interface which consists of a function name, the input parameters and the type of output are defined. It provides a kind of information hiding since the information about the underlying functionalities is contained in the function declaration but hidden from the program using that function. The function can be called on different arguments. In short, we can say that the code can be made generic as well as reusable by enclosing it inside a function.

  • Data Abstraction - The concept of data abstraction refers to hiding the information about the way data is represented. The mechanisms used for data abstraction are abstract data type declarations and modules. By data abstraction, an interface of the data structure can be identified. It also helps in information hiding by the separation of implementation decisions from those parts of the program that are using the data structure. It provides the reusability of data structure by many different programs. An abstract data type provides a specific interface to be used by other parts of a program. It also makes sure that it can only be used through its interface by restricting access to it.

Pure Abstract data type programming is value oriented rather than object-oriented. Object-Oriented programming uses procedural abstraction to achieve data abstraction. Objects are centered around the constructors of a data abstraction. Abstract data types use type abstraction centered around the operations.



History of Object-Oriented programming

  • Simula - Simula is considered as the first object oriented programming language with features like classes, objects, dynamic lookup, subtyping and inheritance. Simula does not have the concept of abstract classes since classes with pure virtual methods can be instantiated. Although Simula 67 cannot distinguish between public and private members of the classes, a later version provided this facility and allowed the attributes to be made protected.
  • Ada - Ada is another language that does provide encapsulation and can be used to simulate abstract data types. These encapsulation constructs are known as packages. Packages can be written with a separate interface called package specification and the implementation is called package body. Ada provides constructs that define the named components for the construction of program systems. Modula-2 is a procedural language which uses modules instead of packages used in Ada to provide support for abstract data types.
  • Modula - In Modula, a module interface is known as a definition module and an implementation,an implementation module. Although Modula-2 was not based on objects, Modula-3 is based on implementing abstract data types using objects. Objects in Modula-3 are similar to the objects in Simula. They both have data fields and methods and single inheritance instead of multiple inheritance.
  • BETA - Another object oriented language that can be considered is BETA which is a modern object oriented language providing powerful abstraction mechanisms. These mechanisms include support for identification of objects, classification and composition. It is a strongly typed language with mechanisms such as class, procedure, function, process, exception, all put together in a single abstraction mechanism called as pattern. This unification helps in a sytematic treatment of all abstraction mechanisms and leads to new possibilities. It also has subpattern, virtual pattern and pattern variable and the objects represent the phenomena. However, BETA does not allow for passive objects as is done in smalltalk, C++ and Eiffel. The design of BETA helps in distinguishing between class and type(patterns) and a module.
  • Eiffel - Eiffel is another language whose design is closely based on OOP theory with a formal support for abstract data types. It is the only O-O language that includes a comprehensive approach to software construction and an environment.
  • CLU - CLU was a key step towards the object-oriented approach but some features of OOP are missing. It consists of three kinds of modules with each module for each kind of abstraction.Procedures support procedural abstraction, iterators support control abstraction, and clusters support data abstraction.The clusters of CLU closely resemble the concept of an object. It made use of constructors for abstract data types. Languages like Python and Ruby borrowed several features from CLU.
  • Scala - Scalais a statically typed language used to combine features of object oriented and functional programming. It supports two styles of abstraction : the functional style that uses parameterization and the abstract type representing the object oriented approach. It contains an expressive static type system which helps in enforcing an appropriate use of abstraction.
  • VB.NET - Visual Basic is an object based language but VB.NET is an object oriented language. The construct that is used to define an abstraction is called a class. To create an abstract class in VB.NET, the class declaration is done as :
                                 MustInherit Class Classname

An abstract class can implement any number of members. The members can either be overridable or can have an implementation common to all the inheriting members.



Creation of objects in Object-Oriented Languages

Java

A class is just like a blueprint that informs the compiler about the attributes and behavior of an entity. This class blueprint is then used to build the object by telling the compiler to make a new object using a constructor for the class. Every class in Java has atleast one constructor. A constructor is a method that has the same name as the class and performs initialization of a new object. A new operator just creates a new but uninitialized instance of the class. Then the constructor method is called , passing the new object (implicitly)with the arguments (explicitly). These arguments are used to do initialization. A number of objects can be created for a given class. For Example : Consider the declaration

                                    Vehicle van;
                                 van = new Vehicle();

The first line is declaring van as a referance to an object of type Vehicle. Thus, we can say that van is a variable that can be used to refer to an object but it cannot be considered as an object. In the first line, it is containing the value null meaning that it does not refer to an object. To link it to an object, we create a new Vehicle object and assign a referance to van to Vehicle. The new operator is used to allocate memory for an object as well as to return a referance to it. Let us consider another example :

                                  public class Point {
                                    public double x, y;                  
                                 public Point(double x, double y) 
                                     {     
                                      this.x = x; this.y = y; 
                                     }        
                                   public double distanceFromOrigin(  ) 
                                     {  
                                    return Math.sqrt(x*x + y*y);    
                                     }
                                    }

We have declared a class Point that is stored in the file Point.java and is compiled to a file Point.class. Now, to hold a Point object ,we need to declare a variable that can be used to hold the Point object.

                                       Point pt;

This declaration of variable does not create an object. To create an object, we use a new operator followed by an object's class and an optional argument list in parenthesis. These arguments are passed to the contructor method for the class, which is used to initialize the fields in new' object. For example: to create an object, we give the following command

                            Point pt = new Point(1.0, 2.0);

There are several ways to create objects in Java. There are some classes in Java whose objects are created using special literal syntax. Java also supports a dynamic loading mechanism which can be used to create objects of those classes dynamically. This is done using the newInstance() methods of java.lang.Class and java.lang.reflect.Constructor.An object that has its state already saved to a file can be recreated using the java.io.ObjectInputStream class. However, using the new keyword is the most popular way to create objects in Java.

This is how we define a class and create an object in Java. Now suppose we have declared a square class which is part of the package shapes. We have to use some more classes like circle, rectangle an so on. These classes can be given a common method area(). So,in such a case, it would be helpful if we combine all these shapes under a common superclass, Shape and we structure this class hierarchy in a way so that each shape object, regardless of its own properties, can be assigned to variables or fields of type Shape. To put in other words, we use our Shape class to encapsulate what all shapes have in common(area()). This is done in Java using the abstract methods. But the Shape class cannot define useful implementations of the methods. This kind of class is known as an abstract class i.e., some of the methods in that particular class can be left unimplemented. Those methods are indicated with the keyword "abstract". These methods are sometimes referred to as subclasser responsibility since they have no implementation specified in the superclass. The method is defined using the following syntax:

                        abstract type name(parameter-list);

For Example :

                        public abstract class AbstractClass
                        {
                        public String toString() {return "An AbstractClass object";}
                        public abstract void visit(Object o);
                        }

The above example has one implemented and one non-implemented abstract method. The declaration of an unimplemented method is similar to the way methods are declared in an interface. The difference is the addition of the "abstract" keyword. An abstract class is said to be totally abstract if it contains all the unimplemented methods. Such a class can extend at most one superclass of Java. There can be no objects of an abstract class which means that an abstract class cannot be instantiated directly with the new operator.Abstract static methods or abstract constructors cannot be declared. A subclass of an abstract class must either implement all of the abstract methods in the superclass, or should be declared abstract. Concrete methods are allowed in abstract classes with as much implementations that can fit. Abstract classes can be used to create object referances since Java's approach to run-time polymorphism is implemented using the superclass referances.


C++

C++ is an extension of C which is based on objects. It was designed with the purpose of providing C with classes. While languages like Simula, Smalltalk, Java only allow the creation of objects on the heap, C++ objects are allocated on the run-time stack. To define an object in C++, a class is first declared. A constructor is used to initialize the member data of an object if a program contains a statement or expression indicating the creation of a new object. The declarations of a class are divided into three parts - private members, protected members and public members. For Example :

                        class object
                         {
                         private:
                         int data;
                        public:
                         void setdata(int d)
                        {data=d;
                          }
                          void showdata()
                         {
                        cout<< "data is "<< data";
                         }
                         };
                          void main()
                          { 
                        object a1, a2;
                         a1.setdata(100);
                         a2.setdata(200);
                         a1.showdata();
                         a2.showdata();
                         }

In the above example, a1 and a2 are the objects of the class object each of which has been assigned a value. When we specify a class, it does not create any object but denotes how they would look when created. The definition actually creates the objects that can be used by the program. It is same as defining a variable of a datatype which seta space in the memory for the variable. The functions setdata() and showdata() are used to provide access to data items from outside the class. The above code gives the output:

                         Data is 100 (displayed by object a1)
                         Data is 200 (displayed by object a2)

To define an abstract class in C++, which does not have any implemented objects of its own, a specific construct is provided. C++ provides abstraction as one of its most powerful features. A programmer has the advantage of abstracting both code and data. The main abstraction mechanisms provided by C++ are classes, inheritance of classes, and templates. An abstract class is one that atleast has one pure virtual function. A pure virtual function is one whose implementation is declared as empty. For Example :

                                class object
                               {
                              public:
                               void virtual Create()=0;
                               void virtal Destroy()=0;
                                };

In the above example Create() and Destroy() are pure virtual functions. We cannot create an object of an abstract class but can declare a pointer with type object*. This pointer can be assigned to any object from any derived class of object. An abstract class in C++ is usually created to define an implementation and is intended to be inherited from the non-abstract classes. For Example : a code to create an abstract class can be written in the following form -

                                 class AbstractClass 
                                {
                                public:
                              virtual void AbstractMemberFunction() = 0;
                              virtual void NonAbstractMemberFunction1();
                              void NonAbstractMemberFunction2();
                            }

If we want to create a non-abstract class from an abstract class, we need to declare and define a matching member function for each abstract member function of the base class. The creation of an interface that can access only certain elements of data types is possible. The division of code into smaller fragments called functions helps in the reusability of code. Abstract classes serve as a base class for other classes and cannot be used to create any objects.


Smalltalk

Smalltalk was the first object-oriented programming language to become popular. It is a dynamically typed, reflective programming language developed with ideas taken from Simula67 and LISP. It was a completely new language with new terminology and an original syntax. The first version of Smalltalk to be made publicly available was Smalltalk-80. In smalltalk, everything is an object, even a class and so all operations are referred to as messages to objects. It is more flexible and powerful than the languages that preceded it like Simula. Smalltalk has the mechanism that allows objects to detect a message they do not understand and to respond to that message as well. A class variable is a variable that is defined in the class and shared among all the objects that are present in the class. Smalltalk is based solely on sending messages to objects. For example: Let us consider the definition of a Point class

                         class name    Point
                         superclass    Object
                          class var     pi
                          instance var  x y
                         class messages and methods
                        (..names and codes for methods)
                          instance messages and methods
                         (...names and code for methods)

In the above example, the value of pi can be used in geometric calculations which involve points. So, the variable pi has been included in the class Point. Every message is followed by some object. Each point object is having two instance variables, x and y. Methods are the functions that can be used to manipulate objects . Messages contain the names and parameters of methods. For Example : The code below shows example class messages and methods for point objects

                         newX:xvalue Y:yvalue ||
                         ^self new x: xvalue y:yvalue
                         newOrigin||
                           ^self new x:0 y:0
                         initialize||
                           pi <- 3.1415

In object-oriented languages like Java, a programmer can decide if a part of the object is to be made visible to clients of the class. In smalltalk, rules have been specified for the programmer to take this decision.

  • The methods are made public. Any code which has a pointer to an object can send any message to it. If that method is defined in the class or superclass, it will be invoked. Thus, all the methods of an object that can access that object are made visible to the code.
  • Instance variables have been made protected. They are accessible only to the methods of the class of the object as well as to the methods of the subclasses. In the handling of primitive numbers and arrays, it makes use of built in abstract data types.


C#

The objects in C# are created on the heap, using the new keyword. For Example :

                                   Name J = new Name();

In this example, J is just a referance to an object It does not contain the value of the object Name but contains the address of the unnamed object that has been created on the heap. When we instantiate an object, a method which is known as a constructor is called. A constructor creates the object which is specified by the class. An alternative method to initialize an object in C# is the object initializer. It uses abstract modifier to support abstract class members and abstract classes. It just requires a property line with the abstract keyword with empty get and set methods. To create an abstract class in C#, the class declaration should be done as:

                                   abstract class Classname;

The abstract class of C# contains abstract members which define what a subclass needs to have. The abstract class establishes what a class is . For Example : an abstract Ball class would lay out the common characteristics of all Balls but we cannot instantiate the abstraction Ball itself. The abstract keyword serves to implement the abstraction Ball that will be manifest in the different concrete instances of Ball such as blue ball or round ball. It establishes what a Ball. In C#, the alternative to using abstract is to define an interface.



Uses

  • Flexibility - It provides flexibility since the programmer can now hide the details or data that are not required for presentation. It allows the specialization of inherited classes.
  • More security - It helps in hiding the implementation details and giving access only to the data.
  • Reducing Complexity - It helps a user to divide a large program into chunks of modules.This helps in making the debugging as well as testing a lot more easier. It provides a means to choose a variation from a common pattern. The complexity is reduced by ignoring unimportant details.
  • Easy replacement - It is easier to replace code without recompiling. New class could be easily added to an existing program without changing the main program.
  • Reusability - It helps in reusing software components in other applications. For example : colour has the methods of darker() and brighter(). Once the idea of colour is abstracted into an object, it can be used for different programs thus reducing the time required in development as well as debugging of the code.



Links

  • Concepts in Programming Languages by Mitchell, John C. (example)
  • Java in a nutshell [electronic resource] by David Flanagan
  • Object Oriented Design using Java by Dale Skrien.
  • Programming C# 3.0, 5th Edition