Object Oriented programming

From Conservapedia
Jump to: navigation, search

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".


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


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.


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 descendant. 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. A descendant 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.


Encapsulation means that the data used by a class is defined within the class itself, rather than the code having to resort to global data. Further, functions specific to the class, called "methods" are also defined within the class.

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".


A method is a function that is encapsulated within a class. Methods have some level of scope and some mechanism for inheritance.

Different languages support different types of scoping. Typically, these levels are:

  • Private: The method is only visible to methods within the class.
  • Protected: The method is only visible to methods within the class or methods in descendant classes.
  • Public: The method is visible to methods within the class or its descendants, and to any code referencing an instance of the class or one of its descendants.

Inheritance mechanisms define how a method interacts with inheritance.

  • Non-virtual: The method is not affected by inheritance. It is simply a method in the class.
  • Virtual: The method can be overridden in a descendant and may be overriding the same method in an ancestor class.
  • Pure virtual: There is no implementation of the method in this class - there is only the method definition. The purpose is for different descendants to override and implement the method appropriately for those classes. Compilers typical generate warnings or errors to code that instantiates a class which has pure virtual methods since calling a pure virtual method will result in an error.

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 instance 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();