Computer Science
Object Oriented Programming

What Is Object Oriented Programming?

Object Oriented Programming differs from structured programming approach. Structured programming involves creating functions and procedures to solve aspects of the problem. The main body of the program is a series of function and procedure calls that pass the relevant data to the procedures. The data and the code that operates on that data are defined separately in structured programming.

When programming using an object-oriented appraoch, data and the code that transforms those data are contained within one unit within the program.

More recent implentations of most high-level imperative languages include object-oriented capability. Some languages, like Java, are fully object-oriented. C# is an object oriented language.

Objects & Classes - The Difference

A class defines the properties (data fields) and methods (procedures and functions - behaviours) that will be used for each object (instance) of that class. A class can be defined as a set of objects with a common data structure and common behaviour.

A class is an abstract data type. Class definitions are similar to type declarations in C#. An example of a user-defined type in C# is, public struct TStudent {
   public string surname;
   public string forename;
   public int mark;
}

Notice that we have combined more than one field into the definition shown above.

A class definition is similar except that it also includes methods (procedure and function definitions). In a class definition, you also describe how methods and properties are accessed from code outside of the class definition.

Fields and methods can be public (can be called from code outside of the class) and private (can only be called or altered by the class). There are other types of access that go beyond our needs at this time.

When a programmer uses a class, they declare an instance of the class. This is called instantiation. The class itself is the abstract definition of how objects of that type should behave.

Example Class Definition

Context: A group of goths have set up a club where goths can go each week to compare clothing and make-up tips as well as sitting in corners looking dark and moody. Slowly but surely the club builds up a sizeable membership and it is decided that a computer system should be used to make it possible to keep in touch with all of the goths and send them details of events like Vampire Awareness Week and the annual Gloomathon. The system is going to be written by Richard Mortis, the blood-spattered would-be leader of this group. He decides that he will take an object oriented approach to the problem (he might only wear black, white and blood-red but he's no slouch with a computer). Each member has a unique membership number, their name and their email address are also going to be stored. Programs that use the GothMember class need to add new members, amend a member's details or display a member's details. No other form of access is required.

class Goth
{
   private int MemberShipNo;
   private string Name;
   private string Email;
   public Goth(int newmemberno, string membername, string email)
   {
      MemberShipNo = newmemberno;
      Name = membername;
      Email = email;
   }
   public void AmendMember()
   {

   }
   public void DisplayMember()
   {

   }
}

Fields are normally declared as private, methods are normally public. The method with the same name as the class is called the constructor. This method is called whenever a new instance of the class is declared. It is common to use parameters in the constructor to set the values of key fields - that is shown in the example.

Goth mygoth = new Goth(1234, "Mr Dark", "paleface@gothcentral.co.uk");

To display the details of the goth we have called mygoth, we would use the following line of code,

mygoth.DisplayMember();

Key Object-Oriented Programming Terms

In addition to the terms object, class, method and field, the following terms need to be understood.

Encapsulation

This can be defined as the technique which combines the methods and fields into one unit. In the goth example, all of the data fields for a goth member are contained within the class definition. To access them, we need to call the class methods for the object that we are working with. This is called data encapsulation - the data fields are private and there is restricted access to them. Procedural encapsulation would be present in a class definition which had private methods.

Inheritance

Imagine that the goths in Goth Club realised that some of their members were under 18 and therefore not allowed to attend certain events in the Goth Calendar. They might decide that although they were all goths frowning together, some of the goths were Senior Goths and some of them were Junior Goths. It is decided that some of the information stored for a junior goth would be the same as for any other goth but that a few extra fields were required. Suppose that the same is true of senior goths too. We need therefore to create a base or parent class called Goth, but two sub-classes, SeniorGoth and JuniorGoth. In OOP we are able to use the concept of inheritance to make it easier to define the classes for these two new object types as well as ensuring some consistency of programming logic between the related items.

The SeniorGoth class may be declared as follows,

class SeniorGoth: Goth
{
   public SeniorGoth(int newmemberno, string membername, string email)
      :base (newmemberno,membername,email)
   {

   }
   public override void DisplayMember()
   {

   }
}

Notice that we write the name of the class we are inheriting next to this class name.

All of the fields and methods that belong to the parent class are automatically inherited. The line directly underneath the contructor declaration calls the constructor of the base class so that the default fields are initialised.

In this example, assume that the DisplayMember() method needs a few tweaks for this subclass. We can override the method in the base class and describe a new set of instructions for this subclass. The keyword override is used in this method to indicate this. In the base class, we change the method declaration as follows to reflect this,

public virtual void DisplayMember()

The following inheritance diagram shows the relationship between our base class and derived classes in this example.

inheritance diagram

Polymorphism

The overridden method shown above is an example of polymorphism. This term refers to the way that subclasses may use the same method name as a parent class but implement them differently.

Why OOP?

Although the move from structured programming to object oriented programming can be a challenge for a programmer, there are many benefits of using this approach to solving problems.

The public fields and methods of a class make up the interface that the programmer uses when they declare and use an instance of a class. In some cases, this can be used to ensure that data held in fields is valid and useable by the methods of the class. With a more complex class definition, setting values for fields may require a series of complicated calculations or operations to be performed in order to arrive at the final value to be stored. The class can disguise some of this complexity from the programmer making it easier to complete the programming task.

Classes are reusable and can be written and shared by all sorts of different people. Programming in .NET and lots of other languages is made easier because of the number of built-in classes that can be used,