Object-oriented programming: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
mNo edit summary
imported>Tom Morris
(gawd this is a mess.)
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=Object oriented programming=
{{subpages}}


==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) was inspired by the branch of mathematics called [[category theory]].


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)
==Object oriented programming languages==
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 object-oriented languages include Java, C#, Ruby and Eiffel. Python, Perl and C++ have objects as an option.


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).
Some languages can easily create object oriented structures from scratch. It helps if functions can be first class elements of the language in question.
[[Geos]] was an object oriented OS written in assembler from scratch
C - Ref: Object-Oriented Programming With ANSI-C
          www.planetpdf.com/codecuts/pdfs/ooc.pdf
[[Erlang_programming_language/Tutorials/erlangOOP|Objects with erlang]]


Strict OOP languages include
== Inheritance ==
Java, C#, Ruby and Eiffel


Some languages have OOP as an option and are not strict.
Child classes inherit attributes and methods from their parent classes. Some languages only allow one parent, these languages are single inheritance languages. Other languages are known as multiple inheritance languages.  
python, perl and C++


Geos was an object oriented OS written in assembler from stratch.
Single inheritance languages include [[Java (programming language)|Java]], [[C#]], [[Ruby (programming language)|Ruby]]


==Inheritance==
Multiple inheritance languages include [[C++]], [[Python]], [[Perl]], [[Eiffel]]. Multiple inheritance can be more complicated for the compiler/interpreter, and also potentially suffers from the [[diamond problem]]. Java and C# allow multiple ''interfaces'' as an alternative to multiple inheritance: this provides extra utility for the programmer but without the overhead of the compiler.


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.  
An alternative to multiple inheritance is to have a two-level inheritance structure where, like with Java and interfaces, you can inherit from a class, but you can also 'mix-in' some other module of code. Ruby allows you to define a class that inherits from a class but also mixes in a module. Scala uses a system of ''traits'', which are basically Java interfaces with concrete default implementations.


Single inheritance languages
Interfaces are like god-parents, they give birthday
java, C#
presents(functionality) but no genetic material. It is often
a good idea to avoid multiple inheritance as much as
possible, even when available, to avoid programming complexity.


Multiple inheritance languages
<!--
C++, python, Ruby, perl, Eiffel


Class inheritance is often described with a Class Diagram.
Class inheritance is often described with a Class Diagram.
Line 43: Line 47:
       |  implements
       |  implements
       |
       |
  +-------------------+
  +-----------------------+
  | Part             |    class name
  | Part                 |    class name
  +-------------------+
  +-----------------------+
  | container_obj     |    attributes
  | container_obj = earth |    attributes
  +-------------------+
  +-----------------------+
  | get_container_obj |    methods
  | get_container_obj     |    methods
  | set_container_obj |
  | set_container_obj     |
  +-------------------+
  +-----------------------+
     / \
     / \
       |  implements
       |  implements
Line 57: Line 61:
  | Container  |    class name
  | Container  |    class name
  +------------+
  +------------+
  | parts     |    attributes
  | parts = [] |    attributes
  +------------+
  +------------+
  | get_parts  |    methods
  | get_parts  |    methods
Line 63: Line 67:
  +------------+
  +------------+


In many lanugages, class names are capitalized, and attributes and methods are lowercase
-->
 
In many languages, 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.  
(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  
Because of inheritance, each container class will include a  
Line 75: Line 81:
and class Part is the parent of class Container.
and class Part is the parent of class Container.


==Default values for attributes==


Often attributes start with some default value. This is very handy because default values can
help one to start programming very complex system quickly. This is the just-add-water
idea of OOPS. In the diagram we see that the container_obj = earth, as a default value.
The default value for parts = [], is an empty list.
Consider the creation of an instance of window. The window can have
many complicated attributes precreated such as scroll bars, default title text, edit
icons and default menus for example. These can be ignored or modified as needs be.
Default values for attributes can make learning and using OOPS superier to other
programming design methods when used well.


==Static variables==
==Static variables==
Line 108: Line 124:
==Overriding==
==Overriding==
When a child class uses a method with the same name as parent's method.
When a child class uses a method with the same name as parent's method.
Both functions should have the same signature.
Both functions should have the same signature. Every time the child's
function has the same signature as the signature of the parent's function,
the child's function over-rides the parent's function. If we have a tall
stack of matching functions, the function at the bottom is used, if
called by an instance of the bottom most child.  


  +--------------+
  +--------------+
Line 125: Line 145:
  | move()->row() |    methods
  | move()->row() |    methods
  +---------------+
  +---------------+
        |   
  _____|_______
  /  rowboat    \
  |  instance_1 |
  \_____________/


==Overloading==
==Overloading==
Line 142: Line 167:
A signature has two main parts and two optional parts.
A signature has two main parts and two optional parts.
  * name of the function
  * name of the function
  * the number of arguments
  * the number of arguments         (perl can ignore the number of arguments)
Optional parts (some languages require these in a signature, it depends on the language)  
Optional parts (some languages require these in a signature, some do not)  
  * the types of the arguments
  * the types of the arguments     (perl can ignore argument type)
  * the return type of the function
  * the return type of the function (perl can ignore return type)
 
References
 
(Java in a Nutshell - by David Flanagan - 2005) - inheritance
(Java in a Nutshell) - overriding - pg 120
(Jave in a Nutshell) - overloading - pg 96

Latest revision as of 05:33, 12 July 2010

This article is a stub and thus not approved.
Main Article
Discussion
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
 
This editable Main Article is under development and subject to a disclaimer.

'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) was inspired by the branch of 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 object-oriented languages include Java, C#, Ruby and Eiffel. Python, Perl and C++ have objects as an option.

Some languages can easily create object oriented structures from scratch. It helps if functions can be first class elements of the language in question.

Geos was an object oriented OS written in assembler from scratch
C - Ref: Object-Oriented Programming With ANSI-C
         www.planetpdf.com/codecuts/pdfs/ooc.pdf
Objects with erlang

Inheritance

Child classes inherit attributes and methods from their parent classes. Some languages only allow one parent, these languages are single inheritance languages. Other languages are known as multiple inheritance languages.

Single inheritance languages include Java, C#, Ruby

Multiple inheritance languages include C++, Python, Perl, Eiffel. Multiple inheritance can be more complicated for the compiler/interpreter, and also potentially suffers from the diamond problem. Java and C# allow multiple interfaces as an alternative to multiple inheritance: this provides extra utility for the programmer but without the overhead of the compiler.

An alternative to multiple inheritance is to have a two-level inheritance structure where, like with Java and interfaces, you can inherit from a class, but you can also 'mix-in' some other module of code. Ruby allows you to define a class that inherits from a class but also mixes in a module. Scala uses a system of traits, which are basically Java interfaces with concrete default implementations.

Interfaces are like god-parents, they give birthday presents(functionality) but no genetic material. It is often a good idea to avoid multiple inheritance as much as possible, even when available, to avoid programming complexity.


In many languages, 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.

Default values for attributes

Often attributes start with some default value. This is very handy because default values can help one to start programming very complex system quickly. This is the just-add-water idea of OOPS. In the diagram we see that the container_obj = earth, as a default value. The default value for parts = [], is an empty list. Consider the creation of an instance of window. The window can have many complicated attributes precreated such as scroll bars, default title text, edit icons and default menus for example. These can be ignored or modified as needs be. Default values for attributes can make learning and using OOPS superier to other programming design methods when used well.

Static variables

Static variables are variables that live at the class level. They are shared by all instances of a class. They can be used to help count and coordinate instances of a class. If varS is a static variable then each instance(instance1,instance2) does not get its own copy of varS, but shares the one copy of varS that lives in the generating Class. For example, each time an instance is created, varS could be incremented. Thereby keeping a count of the instances in existence at any one time.

+--------------------+
| Account            |    class name
+--------------------+
| varS               |
+--------------------+
+--------------------+
      /|\         /|\
       |           |
       |           |
 /----------\   /----------\
| instance_1 | | instance_2 |
 \----------/   \----------/

Static methods

Static methods are functions that live at the class level. They should only use static variables because they do not know about instance variables.

Overriding

When a child class uses a method with the same name as parent's method. Both functions should have the same signature. Every time the child's function has the same signature as the signature of the parent's function, the child's function over-rides the parent's function. If we have a tall stack of matching functions, the function at the bottom is used, if called by an instance of the bottom most child.

+--------------+
| Boat         |    class name
+--------------+
+--------------+
| move()->go() |    methods
+--------------+
     / \
      |
      |
+---------------+
| Rowboat       |    class name
+---------------+
+---------------+
| move()->row() |    methods
+---------------+
       |     
  _____|_______
 /  rowboat    \
 |  instance_1 |
 \_____________/

Overloading

When a class uses more than one method with the same name, but with a different signature, that method is overloaded.

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

Definition of signature:

A signature has two main parts and two optional parts.

* name of the function
* the number of arguments         (perl can ignore the number of arguments)

Optional parts (some languages require these in a signature, some do not)

* the types of the arguments      (perl can ignore argument type)
* the return type of the function (perl can ignore return type)

References

(Java in a Nutshell - by David Flanagan - 2005) - inheritance
(Java in a Nutshell) - overriding - pg 120
(Jave in a Nutshell) - overloading - pg 96