CSC/ECE 517 Fall 2011/ch1 1e lm
5. Block-structured vs. o-o languages. Before o-o languages"took over the world" in the 1980s, the spotlight was on block-structured languages. Why were o-o languages more effective and more durable than block-structured languages? Can block structure be useful in an object-oriented language? Explain.
Introduction
This topic will discuss 2 things viz the effectiveness & durability of object oriented languages (like Java, Python, etc) over block structured languages(like C, Pascal, Algol etc) and how block structure can be leveraged in an object oriented language.
Block structured languages
Definition
A block is consisting of one or more declarations and statements. A block structured programming language is the one that would allow usage of such blocks & including nesting of such blocks. Features of block structured languages: The purpose of blocks is to allow a group of statements to be treated as one. For instance, you might want to have a block of statements which does matrix multiplication.
Blocks also narrow the lexical scope of the variables, procedures & functions declared in a block so that they don’t conflict others(which might be used for a totally different purpose) having the same name with elsewhere in the program. For instance, you might want to have a local variable “count” to count the sum of some variables whereas you might have another local variable with the same name “count” to count the number of elements in an array.
Basic features
- Tasks used to be accomplished using functions. The primary focus was on functions. It involved creating a list of instructions and grouping them into functions.
- The functions shared global data. The data was stored in data structures which acted as information containers. These were global in scope and would be passed around between functions
- It used a top down approach of program design. Here the system is broken down into sub-systems which are recursively refined into additional sub- system levels until we reach base level of granularity which can be implemented by a single function.
- Functions were implemented as black-boxes with an input and output specification.
Examples
Some of the examples of these languages include Algol, C & Pascal.
Hello world! ALGOL Example Program page.
BEGIN FILE F(KIND=REMOTE); EBCDIC ARRAY E[0:11]; REPLACE E BY "HELLO WORLD!"; WRITE(F, *, E); END.
o-o languages motivation
Object Oriented Languages:
Definition
Object oriented languages is the programming paradigm that bundles the data and the methods that access and manage this data into single entity called an “Object”. This bundling of data and methods allows the object to have a fluid interface through which it can interact with the outside world. The object tis generally modeled on real world objects.
Fundamental features of OO languages
- Object/Class: Class consists of tightly coupled data fields and set of operations that act on the data. The entire set of data and operations represent a user-defined data type. An object is an instance of a class. Thus a class acts as a collection of objects of similar type. Multiple objects may be created out of a class. The objects share the operations of the class however they have their own set of data fields. An object can be defined by its current state (current value of its data members) and its behavior (the methods that can be called on the object).
- Data Abstraction/Encapsulation: The wrapping of data and functions into a single unit (class) is called encapsulation. This allows restricting access to data fields and certain methods from external entities. Encapsulation protects an object from accidental modifications by an external entity since data fields can be accessed only through predetermined interfaces. Encapsulation is implemented using language constructs like private, public, protected, etc.
- Abstraction involves representing only the essential features of the object and hiding the background details. Classes use abstraction by hiding the data attributes and providing functions methods that provide a controlled view of the object to an external entity.
- Inheritance: Inheritance is the ability of a class to inherit partly or completely the features of another class. It allows the hierarchical classification of objects.
- For example:
- Inheritance allows the reusability of code. Pre-existing classes can be reused by extending them and adding new functionality to create sub classes without modifying the original classes. The sub class will then have the combined features of both classes. This allows a programmer to tailor a new class from an existing class without introducing any undesirable side effects in the base classes.
- Interface: Interface is an abstract type containing no data but only a list of method definitions. A class that has all the methods defined in the interface is said to have implemented the method interface.
Interfaces help in message passing. Interfaces allow classes to define how they will communicate with an external entity. By implementing an interface, a class promises an external entity using the interface that it will provide the functionality specified by the interface.
- Polymorphism/Dynamic Binding: This is the ability of a variable, method or an object to take more than one forms. This enables an objects of different types to respond to same method or property call but in its own type specific way. For example consider overloading the + operator, so that when called on strings, it concatenates the strings while for numbers it adds them up.
- Polymorphism also allows objects with different internal structures to share the same external interface. This helps in dynamic binding. Dynamic binding means that the code executed for a given function call is not known until actual runtime. For example, in a dynamically typed language a variable can hold an object irrespective of its data type. Hence a function call associated with a reference (variable) depends on the dynamic type of that reference (variable) i.e. the current object referenced by the variable.
Examples:
The below languages implement different degrees of OO features
- Ruby, Scala, Smalltalk, Eiffel are said to be purely OO languages. Here even primitives are impememnted as OO objects.
- Java, C# , VB.NET, Python are OO languages but have some degree of procedural elements
- Visual Basic, PHP, Perl are basically procedural languages with some OO features.
o-o advantages over block-structured
usefulness of block-structured in o-o with examples
Advantages of Object-oriented languages over Block Structured languages
- Object oriented approach follows a data centered approach. It is possible to map objects in the problem domain to those in the program. This helps the programmer visualize the program in terms of real world objects and which leads to a much better design. Also it allows more details of a model to be captured in implementable form.
- The principle of data hiding helps in building much secure program. The data fields of an object can be accessed only through the interface defined by its publicly accessible methods. Such controlled access prevents any inadvertent change to object data by external modules.
- Mapping real world entities into objects help reduce and easily manage software complexity . Also it helps when systems need to be upgraded to larger systems.
- Objects act as black boxes with internal structure and well defined interfaces. Hence adding new functionality becomes easier, since we can easily isolate the objects/classes that need to be modified or extended. The low coupling between objects allows an object to be modified without any effect on the other objects accessing it so long as the interface thought which they interact remain untouched.
- Most modern object oriented languages flaunt a load of class libraries and frameworks. These are much easier to use as compared to library functions present in block structured languages. The frameworks help in rapid software development and easier testing and debugging thus leading to increased productivity.
- Message passing techniques (ex: interfaces in JAVA) help separate implementation of an object from its externally visible interface. Multiple classes of objects may implement the same interface. In such cases an object can be replced by another object without affecting external entities dependent on that object so long as the interface is untouched. This results in low coupling.
- Object oriented design provides much better memory management. Recent OO languages sport automated garbage handling. Even for languages without this concept, memory deallocation of data members can be handled in the destructors of the objects.
- Localised namespaces
Drawbacks of Object-oriented Languages
Although object oriented languages sport some very advantageous features as compared to block structured languages, they do have some <ref name ="drawbacks"><url=http://lucacardelli.name/Papers/BadPropertiesOfOO.html/></ref>
- Economy of language features: The addition of OO features to a languages and the additional syntax needed to implement the features may any OO language bulky. The complexity of these languages makes them daunting to learn and master.
- Economy of execution: OO style is intrinsically less efficient as compared to block structured style. In procedural languages, type checking occurs at compile time. Hence no tests are needed at runtime for pointer dereferencing for method calls.In OO style each routine is supposed to be a virtual method. This introduces indirections in the form of method tables during for dereferencing.
- Also many OO languages include automatic garbage collection. This along with the overhead of creation and destruction of objects degrades performance in resource intensive systems such as embedded systems and real time systems. [Csjournals.com/IJCSC/PDF1-2/9..pdf]
References: </references>