Difference between revisions of "Object Oriented programming"

From Conservapedia
Jump to: navigation, search
m (top: clean up)
(Expanded and clarified)
Line 1: Line 1:
'''Object Oriented programming '''(OOP) languages are based around the paradigm of objects that interact with each other to produce functionality. There are many [[programming language]]s that are designed around object oriented principles, such as [[Java]].
+
'''Object Oriented programming '''(OOP) languages are based around the paradigm of objects that interact with each other to produce functionality. There are many [[programming language]]s that are designed around object oriented principles, such as [[Java]]. A class is a definition of an object and contains data and/or functions.  Functions within a class are called "methods". 
  
== Objects ==
+
==Definition==
Objects are components of an object oriented language, consisting of properties and methods.  
+
Object Oriented programming is defined by three things: inheritance, polymorphism, and encapsulation.
  
An object is also known as a Type or Class.
+
===Inheritance===
 +
Inheritance is the ability for one class to use the data and methods of an ancestor class (or multiple ancestor classes if multiple inheritance is allowed), rather than having to define those again within a new class.  If class B inherits from class A, then class A is called an ancestor class of B, and class B is called a descendant class of A.  A decedent class instance can be used in place of any ancestor class instance, but an ancestor class instance cannot be used in place of a descendant class instance.  
  
== Polymorphism ==
+
=== Polymorphism ===
Polymorphism allows an object to appear as another object. This means that a complex object can appear to be a simple one. The example below shows that you can have a specific model of car, such as the [[Model T]], which looks like and acts like a normal car.
+
Polymorphism allows an object to be used in place of an ancestor object without the code that is using it having to know about the descendent. This means that a complex object can appear to be a simple one. The example below shows that you can have a specific model of car, such as the [[Model T]], which looks like and acts like a normal car.  Polymorphism can be implemented in different ways.  Smalltalk implemented it via messages.  Most compilers that support OOP implement a virtual method table, which is an array of method pointers as per the CORBA standard.
  
=== Example (Java)===
+
===Encapsulation===
 +
Encapsulation means that the data used by a class is defined within the class itself, rather than having to resort to global data.
  
 +
== Classes and Instances ==
 +
A class is a definition.  In most languages, a class is a data type.  An instance is an instantiation of a class.  It can be considered in terms of a real-world analog: the designs for a pickup truck are akin to a class.  An actual pickup truck build according to those designs is an instance of that class.  The term "object" is often used to refer to either the class, an instance, or the general concept.  The data within an instance is referred to as "instance data".
 +
 +
==Constructors and Destructors==
 +
A constructor is a class method that is automatically called when an instance of the class is instantiated.  The purpose of the constructor is to set up the new instance.  A destructor is a method that is called when an instance of the class is destroyed.  The purpose of the destructor is to clean up just prior to the class being deleted.  Typically this includes freeing memory pointed to by instance data.  Some programming languages support special constructors called "copy constructors" which are called when an instance is created as a copy of another instance.
 +
 +
== Example (Java)==
 
<pre>
 
<pre>
 
public class ModelT extends Car {
 
public class ModelT extends Car {

Revision as of 20:26, December 12, 2019

Object Oriented programming (OOP) languages are based around the paradigm of objects that interact with each other to produce functionality. There are many programming languages that are designed around object oriented principles, such as Java. A class is a definition of an object and contains data and/or functions. Functions within a class are called "methods".

Definition

Object Oriented programming is defined by three things: inheritance, polymorphism, and encapsulation.

Inheritance

Inheritance is the ability for one class to use the data and methods of an ancestor class (or multiple ancestor classes if multiple inheritance is allowed), rather than having to define those again within a new class. If class B inherits from class A, then class A is called an ancestor class of B, and class B is called a descendant class of A. A decedent class instance can be used in place of any ancestor class instance, but an ancestor class instance cannot be used in place of a descendant class instance.

Polymorphism

Polymorphism allows an object to be used in place of an ancestor object without the code that is using it having to know about the descendent. This means that a complex object can appear to be a simple one. The example below shows that you can have a specific model of car, such as the Model T, which looks like and acts like a normal car. Polymorphism can be implemented in different ways. Smalltalk implemented it via messages. Most compilers that support OOP implement a virtual method table, which is an array of method pointers as per the CORBA standard.

Encapsulation

Encapsulation means that the data used by a class is defined within the class itself, rather than having to resort to global data.

Classes and Instances

A class is a definition. In most languages, a class is a data type. An instance is an instantiation of a class. It can be considered in terms of a real-world analog: the designs for a pickup truck are akin to a class. An actual pickup truck build according to those designs is an instance of that class. The term "object" is often used to refer to either the class, an instance, or the general concept. The data within an instance is referred to as "instance data".

Constructors and Destructors

A constructor is a class method that is automatically called when an instance of the class is instantiated. The purpose of the constructor is to set up the new instance. A destructor is a method that is called when an instance of the class is destroyed. The purpose of the destructor is to clean up just prior to the class being deleted. Typically this includes freeing memory pointed to by instance data. Some programming languages support special constructors called "copy constructors" which are called when an instance is created as a copy of another instance.

Example (Java)

public class ModelT extends Car {
    private Color color = Color.BLACK;
    private String model = "T";

    public void setColor(Color color){
        this.color = color;
    }

    public Color getColor(){
        return this.color;
    }

    public String getModel(){
        return model;
    }
}

public abstract class Car {
    void setColor(Color color);
    Color getColor();
    String getModel();
}