Programming Languages (CS302 2006S)

Basics of Object-Oriented Problem-Solving

Comments on:

The questions:

Comments from: Peter Brown, Michael Claveria (*), Davis Hart, Alex Leach, (late), Brad Miller, Angeline Namai, Mark Nettling, Dimitar Tasev, Dave Ventresca (*).


What is object-oriented programming?

object-oriented programming: The idea behind object-oriented programming is that a computer program may be seen as composed of a collection of individual units, or objects, that act on each other, as opposed to a traditional view in which a program may be seen as a collection of functions or procedures, or simply as a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects. From Wikipedia

Object-oriented programming: A method of programming in which programs are represented by objects. Objects contain methods and procedures which can manipulate the object or its data, and work in conjunction with other objects to produce the result of the program. Advanced features allow classification of objects to better represent the problem (i.e. circle objects are shape objects with additional parameters).

I think you mean e.g., rather than i.e..

Object-Oriented Programming (OOP) A class of programming languages and techniques based on the concept of an “object” which is a data structure encapsulated with a set of routines which operate on the data. OOP supports encapsulation (the ability of an object to keep some or all of it's data and/or methods in private scope), inheritance (the ability of an object to derive behavior and data from other classes - supports both the reuse and recycling of code as well as maintainability) and polymorphism (the ability of an object or function to take many different types as arguments, one such function is - in C, C++ or Java - the ++ function (can take arguments of ).

Object Oriented Programming - The use of a class of programming languages and techniques based on the concept of an "object" which is a data structure (abstract data type) encapsulated with a set of routines, called "methods", which operate on the data. Operations on the data can only be performed via these methods, which are common to all objects that are instances of a particular "class". (courtesy dictionary.com)

Object oriented programming is a style of programming which requires the programmer to create and use data-laden "objects" to store and modify data instead of the function/procedure view of programming. Objects have attributes and behaviors and can often belong to an inheritance hierarchy to further describe their properties.

Question #1 ( from http://en.wikipedia.org/wiki/Object-oriented_programming_language )

An object-oriented programming language (also called an OO language) is one that allows or encourages, to some degree, object-oriented programming methods.

Simula (1967) is generally accepted as the first language to have the primary features of an object-oriented language. It was created for making simulation programs, in which what came to be called objects were the most important information representation. Smalltalk (1972 to 1980) is arguably the canonical example, and the one with which much of the theory of object-oriented programming was developed.

OO languages can be grouped into several broad classes, determined by the extent to which they support all features and functionality of object-orientation and objects: classes, methods, polymorphism, inheritance, and reusability.

Object-oriented programming is programming based on objects. An object is a collection of data, called fields, and the operations that the object can perform, called methods. Objects are often categorized into classes, and they may interact with other objects because they are capable or receiving and sending instructions. Thus, in OOP, an object is the basic unit, and a procedure is seen as a subunit of an object, as opposed to functional programming where functions are the basic unit. bstraction and encapsulation are used to increase code clarity, quite independent of the other two traits. [Sources: www.wikipedia.org; http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2004F/]

The idea behind object-oriented programming is that a computer program may be seen as composed of a collection of individual units, or objects, that act on each other, as opposed to a traditional view in which a program may be seen as a collection of functions or procedures, or simply as a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects. Object-oriented design supports the concepts of encapsulation, inheritance, and polymorphism. [Sources: www.innovatia.com/software/papers/com.htm; en.wikipedia.org/wiki/Object_Oriented_Programming]

A type of programming in which data structures are defined in addition to the data itself. The data and the functions that are applied to the data are encapsulated in an object. These objects are able to inherit characteristics from other objects, making it easier to modify existing programs.

What is the difference between polymorphism and overloading?

Overloading is a type of polymorphism, it alows different functions to be called the same thing, the type of data passed to it is what determines which function will get used. In polymorphism one name is used for different types of data.

It seems to me that the primary difference between polymorphism and overloading is that overloading is a more specific type of polymorphism that requires the programmer to explicitly defines the extra uses for a function. Overloading in general, however, is just the idea of using a function with multiple data types, whether explicitly defined to handle extra types or not.

Not quite; most computer scientists consider them very different.

Polymorphism - a variable that may refer to objects whose class is not known at compile time and which respond at run time according to the actual class of the object to which they refer (variable are usually described as a parent class in the code) (courtesy dictionary.com)

Overloading - a type of polymorphism where different functions with the same name are invoked based on the data types of the parameters passed. (courtesy wikipedia.org)

That pair of definitions does not really speak explicitly to the difference.

Polymorphism allows the programmer to substitute a member of a subclass for a member of a superclass. As the name suggests, a subclass inherits all fields and methods from the superclass, and may have some additional attributes and operations. In OOP, the programmer may also define different procedures using the same name. The appropriate procedure will then be invoked based on the context, or data type of the parameter passed. This is called overloading. (Sources: www.wikipedia.org http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2004F/)

Two definitions, but no explicit discussion of their relationships.

olymorphism vs. overloading:

Polymorphism assures that any class derived from a certain parent will have the same methods as that parent class. This allows the programmer to call certain methods on an object without knowing the exact type of the object. Overloading allows the programmer to define multiple methods differentiated by the number or type of parameters in the call. The compiler or interpreter will determine the correct code to run when the method is called based on the signature of the method. [Wikipedia, http://en.wikipedia.org/wiki/Polymorphism_(object-oriented_programming)]

Again, no explicit discussion of relationships.

Question #2 (from http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 )

Although this pair speaks to some difference, it seems limited.

Polymorphism in computer science terms is allowing a single definition to be used with different data types specifically different classes of objects. For instance, a polymorphic function definition can replace several type-specific ones, and a single polymorphic operator can act in expressions of various types. The concept of polymorphism applies to data types in addition to functions. A function that can evaluate to and be applied to values of different types is known as a polymorphic function. A datatype that contains elements of different types is known as a polymorphic datatype.

Overloading is a specific type of Polymorphism, more specifically it is a type of ad-hoc Polymorphism. Ad-hoc polymorphism is exhibited by functions that operate on a finite set of different and potentially unrelated types, or on datatypes that contain a finite set of elements of different types. Overloading allows the use of functions or methods with the same name but with different arguments. The decision on which function to use is done by the compiler when the program is compiled and there is no dynamic binding. Using overloading, it is possible to have a function perform two completely different things based on the type of input passed to it; this is not possible with parametric polymorphism.

[Sources http://www.answers.com/polymorphism; http://en.wikipedia.org/wiki/Polymorphism_(computer_science); www.cs.ust.hk/~dekai/151/lectures/l15_overriding.pdf]

Polymorphism refers to applying a single type to multiple kinds of data. Overloading is a type of polymorphism, but is compiler-specific. That is, the compiler automatically figures out which type is needed for a specific instance.

I'm not sure what you mean by type in the first or third sentence.

The Difference between Polymorphism and Overloading is that Polymorphism indicates that the arguments of a SINGLE function can be of many different types, whereas Overloading indicates that there may be many different functions refered to by a single symbol. In overloading there will be many different algorithms with the SAME NAME each taking different types; the appropriate algorithm is chosen according to the type of the arguments given. In Polymorphism (meaning multiple forms) there will be only ONE function that does not require type-specific arguments.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed May 10 09:03:18 2006.
The source to the document was last modified on Mon Feb 27 08:25:40 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS302/2006S/Readings/oop-basics.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu