CSC/ECE 517 Fall 2009/wiki2 15 sm
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.The principle of abstraction is an essential element of Object-Oriented programming.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.
Overview
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 thet defines the program component.
The interaction between these two entities is usually restricted to a specific interface.
Types of Abstraction
There are two kinds of abstraction :
- Procedural Abstraction
- Data Abstraction
Implementation of Abstraction in Object-Oriented Languages
Java
while declaring an abstract class in Java, 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++
Abstraction in C++is amongst its one of the most powerful features.The programmer has the facility to abstract both data and code if needed. Abstract classes serve as a base class for other classes and cannot be used to create any objects. For Example :
class AbstractClass { public: virtual void AbstractMemberFunction() = 0; virtual void NonAbstractMemberFunction1(); void NonAbstractMemberFunction2(); }
An abstract class is usually created to define an implementation and is is intended to be inherited from the non-abstract classes.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.A pure abstract class is a class that has all the functions as virtual and there is no data.
Uses
- Flexibility - It provides flexibility since the programmer can now hide the details or data that are not required for presentation.
- More security - It helps in hiding the implementation details and giving access only to the data.
- Modularity - 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.
- Easy replacement - It is easier to replace code without recompiling.