Object-Oriented Programming: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(49 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=


* [[Programming#Object-Oriented_Programming|Programming]]
* [[Programming_Languages_Concepts#Object-Oriented_Programming|Programming Languages Concepts]]
* [[Actor Model#Overview|Actor Model]]
* [[Actor Model#Overview|Actor Model]]
* [[UML]]
* [[Go Language Object Oriented Programming|Object-Oriented Programming in Go]]


=Overview=
=Overview=
Object-oriented programming is first and foremost about message passing between [[#Class|class]] [[#Instances|instances]].
=Object-Oriented Analysis and Design=
In software development, design is often assumed to happen '''before''' programming. In reality, analysis, programming and design ten to overlay, combine and interweave. '''Object-oriented analysis''' is the process of looking at a problem, system or task, with the intention of creating an application that addresses it, and identifying the objects and interactions between objects. The analysis stage is about '''what''' needs to be done.
The output of the analysis stage is a set of '''requirements'''.
'''Object-oriented design (OOD)''' is the process of converting the requirements into an '''implementation specification'''. The designer names the objects, defines their behaviors and formally specifies which objects can activate specific behaviors on other objects. This stage is about '''how''' things should be done.
The output of the design stage is the implementation specification.
In most cases, all these stages overlap and the development happens in an iterative model, in which a small part of the project is modeled, designed and programmed, and then the program is iteratively reviewed and expanded.


=Object-Oriented Programming vs Functional Programming=
=Object-Oriented Programming vs Functional Programming=


{{Internal|Object-Oriented Programming vs Functional Programming|Object-Oriented Programming vs Functional Programming}}
{{Internal|Object-Oriented Programming vs Functional Programming|Object-Oriented Programming vs Functional Programming}}
=Encapsulation=
Encapsulation is grouping together data and functions that are related to each other. The formal definition of such a bundle of data and functions is a type or a [[#Class|class]].
=Class=
A class represents a type, or a kind, of thing and concept. Classes [[#Encapsulation|encapsulate]] '''state''' - what kind of information, or attributes they can store. Classes also define '''behavior''', or methods. Classes are blueprints for [[#Object|objects]].
=Objects=
An object is a specific instance of a [[#Class|class]].


=Inheritance=
=Inheritance=


=Organizatorium=
The inheritance implies that there are classes that are very generic, and other classes that are specific. The specific classes can inherit attributes and behavior from generic classes, and they can also have specific attributes and behaviors that are particular to them, and to no one else. Thus, classes form hierarchies.
 
Parent types are called <span id='Superclass'></span>'''superclasses'''. Subtypes are called <span id='Subclass'></span>'''subclasses'''. The subclass extends the superclass.
 
The motivation behind inheritance is code reusability. If something that is generally applicable to a set of classes, that belongs into the superclass, which can be then inherited.
 
Also, inheritance, together with [[#Overriding|overriding]] helps implement [[#Polymorphism|polymorphism]].
 
=Overriding=
A [[#Subclass|subclass]] overrides a method inherited from the [[#Superclass|superclass]] when re-defines a methods, while preserving the signature.
 
=Polymorphism=
Polymorphism implies behavior that is '''identical across implementations at a higher level of abstraction and at the same time different at a lower level of abstraction'''. For example, computing the area of a geometrical shape means the same thing across different geometrical shapes like circle, triangle, rectangle - the user wants to know the area - while it is meaning is very different at the lower level that actually computes the area for a particular shape. In other words, polymorphism is a way of establishing an abstraction.
 
At a programming language level, polymorphism is a feature of a programming language allowing a method to behave differently depending on the runtime state at a specific moment in time, specifically the particular object instance the method is being invoked on. The contract of the behavior is defined by an '''interface''', while the implementation of the interface may vary. In Java, different classes may implement an interface, and instances of those classes can be used interchangeably "as" that interface. In Go, different concrete types may implement an [[Go_Interfaces#Overview|interface]].
 
In standard object-oriented languages like Java or Python, polymorphism is implemented by combining [[Object-Oriented_Programming#Inheritance|inheritance]] and [[#Overriding|overriding]].


Object-oriented programming is first and foremost about message passing.
Go does not have inheritance, so it implements polymorphism differently, as shown in [[Go_Language_Object_Oriented_Programming#Polymorphism|Go Polymorphism]].

Latest revision as of 17:15, 30 August 2023

Internal

Overview

Object-oriented programming is first and foremost about message passing between class instances.

Object-Oriented Analysis and Design

In software development, design is often assumed to happen before programming. In reality, analysis, programming and design ten to overlay, combine and interweave. Object-oriented analysis is the process of looking at a problem, system or task, with the intention of creating an application that addresses it, and identifying the objects and interactions between objects. The analysis stage is about what needs to be done.

The output of the analysis stage is a set of requirements.

Object-oriented design (OOD) is the process of converting the requirements into an implementation specification. The designer names the objects, defines their behaviors and formally specifies which objects can activate specific behaviors on other objects. This stage is about how things should be done.

The output of the design stage is the implementation specification.

In most cases, all these stages overlap and the development happens in an iterative model, in which a small part of the project is modeled, designed and programmed, and then the program is iteratively reviewed and expanded.

Object-Oriented Programming vs Functional Programming

Object-Oriented Programming vs Functional Programming

Encapsulation

Encapsulation is grouping together data and functions that are related to each other. The formal definition of such a bundle of data and functions is a type or a class.

Class

A class represents a type, or a kind, of thing and concept. Classes encapsulate state - what kind of information, or attributes they can store. Classes also define behavior, or methods. Classes are blueprints for objects.

Objects

An object is a specific instance of a class.

Inheritance

The inheritance implies that there are classes that are very generic, and other classes that are specific. The specific classes can inherit attributes and behavior from generic classes, and they can also have specific attributes and behaviors that are particular to them, and to no one else. Thus, classes form hierarchies.

Parent types are called superclasses. Subtypes are called subclasses. The subclass extends the superclass.

The motivation behind inheritance is code reusability. If something that is generally applicable to a set of classes, that belongs into the superclass, which can be then inherited.

Also, inheritance, together with overriding helps implement polymorphism.

Overriding

A subclass overrides a method inherited from the superclass when re-defines a methods, while preserving the signature.

Polymorphism

Polymorphism implies behavior that is identical across implementations at a higher level of abstraction and at the same time different at a lower level of abstraction. For example, computing the area of a geometrical shape means the same thing across different geometrical shapes like circle, triangle, rectangle - the user wants to know the area - while it is meaning is very different at the lower level that actually computes the area for a particular shape. In other words, polymorphism is a way of establishing an abstraction.

At a programming language level, polymorphism is a feature of a programming language allowing a method to behave differently depending on the runtime state at a specific moment in time, specifically the particular object instance the method is being invoked on. The contract of the behavior is defined by an interface, while the implementation of the interface may vary. In Java, different classes may implement an interface, and instances of those classes can be used interchangeably "as" that interface. In Go, different concrete types may implement an interface.

In standard object-oriented languages like Java or Python, polymorphism is implemented by combining inheritance and overriding.

Go does not have inheritance, so it implements polymorphism differently, as shown in Go Polymorphism.