Theory of Programming Languages/Object Oriented Programming

From Wikiversity
Jump to navigation Jump to search

This is a lesson in in the course, Theory of Programming Languages, which is a part of The School of Computer Science

Objective[edit]

Books-aj.svg aj ashton 01f.png

Students will learn the history of object oriented programming, and will understand the key features that make it distinct.

Students will be able to answer:

  1. What is notable about object oriented programming?
  2. What problems did object oriented languages solve?
  3. What modern languages are object oriented?

Object-Oriented Programming[edit]

Some programmers compare writing in object oriented languages to making cookie cutters, where the objects created are designed from "molds".

Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods – and their interactions to design applications and computer programs. Programming techniques may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance.

Object-oriented programming is a paradigm first developed in the 1960's that has many unique characteristics. It was not commonly used in mainstream software application development until the early 1990s. Many modern programming languages now support OOP. If you are currently learning programming, it is likely that you are learning languages which are at least partially object oriented. (Examples include: Java, C++, Python)

One of the first unique characteristics of this paradigm is the use of "objects". Objects are data structures which contain "fields" and "methods" to describe their content and functionality. Objects themselves correspond to types, which often correspond to the data relevant to the methods it contained. For example, a "person" object might contain a name and a function which could print that name. Multiple objects can be created from one type, "Steve" and "Martha" might both be people but contain different data.

Let's consider that as an example: (This is written in Java)

public class Person{
 String name = ""

 public Person(String n){
  name = n;
 }

 public String getName(){
   return(name);
 }
 public printName(){
   System.out.println(name);
 }

}

Person steve = new Person("Steve"); 
Person martha = new Person("Martha");

The class "Person" is like a cookie cutter, where we design what a person contains. Then we can use that class to make objects - in this case the objects steve and martha.

Programs written in other non object-oriented languages (such as BASIC) are often only long strings of commands. These strings may be broken up into subroutines, but the code remains accessible from all other parts of the program (global in scope). This can cause bugs to have wide reaching ramifications. Object-oriented languages can bundle up data into discrete objects, and thereby evade this problem. The data is accessed by calling specially written methods which can modify or retrieve information from other sections of code. These act as a kind of intermediary, which quarantines different sections of code.

By concealing the actual nitty gritty programming behind methods used to access data, programming can be made much easier to read and contribute to - you don't need to know exactly how a program might read text from a file, but you can write an object which utilizes that functionality to check the spelling on the text. This concept is called abstraction. This offers an additional boon in that command names may be reused, programmers need not think of brand new function names when contributing to a large project.

Lets enumerate through the core ideas behind Object-oriented programming:

Encapsulation[edit]

Encapsulation is a twofold notion. First, it is the process by which data is bundled together with operators (in the example above, the Person object had the data Name and a method sayName which printed that data). Secondly, it is the hiding or limiting of access to data so as to ensure security,

Consider this example from Wikipedia, which displays the limiting of access to secure data:

namespace Encapsulation
{
   class Program
   {
       public class Account
       {
           private decimal accountBalance = 500.00m;

           public decimal CheckBalance()
           {
               return accountBalance;
           }
       }

       static void Main()
       {
           var myAccount = new Account();
           var myBalance = myAccount.CheckBalance();

           // This Main method can check the balance via the public
           // "CheckBalance" method provided by the "Account" class 
           // but it cannot manipulate the value of "accountBalance"
       }
   }
}

As the comments explain, because the balance of the account is labeled as private, it cannot be modified.

Polymorphism[edit]

Object inheritance (or delegation)[edit]

Open recursion[edit]

Classes of objects[edit]

Instances of classes[edit]

Methods which act on the attached objects[edit]

Message passing[edit]

Abstraction[edit]

Object-oriented programming language examples[edit]

Assignments[edit]

Crystal Clear app kedit.svg
  1. In pseudocode (or an OO language) write a series of objects which inherit characteristics from other objects. For example: a house object which inherits "building material" from a "building" object, and inherits size from "thing" object.
  2. Write a list of five well known object oriented programming languages.
  3. Write a list of five problems which are well suited for object oriented programming languages.

Smiley green alien cry.svg Completion status: this resource is a stub, which means that pretty much nothing has been done yet.