Object-oriented programming: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
imported>Eric Evers
Line 1: Line 1:
=Object oriented programming=
==Overview==
Object oriented programming is a technique to organize data and functions in a computer program. Specifically, data structures called objects are instantiated(created) from templates called classes. Attributes(member variables) and methods(member functions) are hidden inside of objects. Sometimes the attributes and methods are private and available only inside a class. Othertimes some attributes and methods are public and available to other classes or objects. OOP(object oriented programming)
is was inspired by the branch mathematics called [[category theory]].
==Object oriented programming languages==
Some programming languages have object oriented structures build into them. Many languages can create object structures from scratch if needed. C++ has objects built in to it. One can program using objects with pure C, but only if you build your own objects from scratch. Some languages are pure object languages and strictly require that all code be an OOP(object oriented program).
Strict OOP languages include
Java, C#, Ruby and Eiffel
Some languages have OOP as an option and are not strict.
python, perl and C++
Geos was an object oriented OS written in assembler from stratch.
==Inheritance==
Child classes inherit attributes and methods from their parent classes. Some languages only allow one parent, these languages are single inheritance languages. This compares to others that are multiple inheritance languages.
Single inheritance languages
java, C#
Multiple inheritance languages
C++, python, Ruby, perl, Eiffel
Class inheritance is often described with a Class Diagram.
Example Class Diagram
+------------+
| Object    |    class name
+------------+
| id        |    attributes (variables)
+------------+
| get_id    |    methods (member functions)
| set_id    |
+------------+
    / \
      |  implements
      |
+-------------------+
| Part              |    class name
+-------------------+
| container_obj    |    attributes
+-------------------+
| get_container_obj |    methods
| set_container_obj |
+-------------------+
    / \
      |  implements
      |
+------------+
| Container  |    class name
+------------+
| parts      |    attributes
+------------+
| get_parts  |    methods
| set_parts  |
+------------+
In many lanugages, class names are capitalized, and attributes and methods are lowercase
(java for example) In this class diagram, each container is a part, and each part is an object.
Because of inheritance, each container class will include a
container_obj attribute and an id number attribute inherited from their ancestors.
Typically, the parts attribute would need to be a list or an array.
The length of the list of parts would start as empty.
The arrow edges connecting children to parents are sometimes labeled with:
implements. Arrows in diagrams point in the direction of dependency.
The class Object is the parent of the class Part,
and class Part is the parent of class Container.
==Overriding==
When a child class uses a method with the same name as parent's method,
the child's method overrides the parents method.
Class Diagram
+-------------------+
| Boat              |    class name
+-------------------+
+-------------------+   
| move()->propel()  |    methods 
+-------------------+
    / \
      |  implements
      |
+-------------------+
| Rowboat          |    class name
+-------------------+
+-------------------+
| move()->row()    |    methods
+-------------------+
==Overloading==
==Overloading==
  When a child class uses more than one method with the same name,
  When a child class uses more than one method with the same name,

Revision as of 00:44, 5 November 2008

Overloading

When a child class uses more than one method with the same name,
one is overloading a function. Each function needs to have a 
different signature.
Class Diagram

+-------------------+
| Computer          |    class name
+-------------------+  
+-------------------+    
| sqrt( {A,B,i} )   |    methods  
| sqrt(  A  )       |    
+-------------------+

Definition of Signature
The signature of a function is the unique minimal description of a
function from the outside. Looking at a function from the outside
is sometimes called the interface of a function. The signature of 
a function is 
  * the name of the function
  * the number of arguments
Some languages include in the signature of a function the:
  * the type of the arguments
  * the return type is included in the signature