Understanding Object Oriented Programming

What is Object Oriented Programming?

Quite often I get a question or see a question posted online along the lines of: “What is this thing called OOP?”, “What is so special about it?”, “Why should I use it?” or “How do I use it?”. The person asking this type of question usually has experience of non-OO programming and wants to know the benefits of making the switch. Unfortunately most of the replies I have seen have been long on words but short on substance or example.  The main reason for this is that some of the explanations about OO are rather vague and can be interpreted in several ways, and if something is open to interpretation it is also open to a great deal of misinterpretation.  Hopefully this article will help clear up the confusion and give you a better idea of what Object Oriented Programming is.

What is Object Oriented Programming?

Object Oriented Programming is programming which is oriented around objects, thus taking advantage of Encapsulation, Polymorphism, and Inheritance to increase code reuse and decrease code maintenance.  Here are some examples between object oriented programming and non-object oriented programming.  Don’t let the big words scare you – these are easy concepts to learn.

What is an Object Oriented language?

A computer language is an Object Oriented language if it provides support for the following:

  • Class: A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind.
  • Object: An instance of a class. A class must be instantiated into an object before it can be used in the software. More than one instance of the same class can be in existence at any one time.
  • Encapsulation: The act of placing data and the operations that perform on that data in the same class. The class then becomes the ‘capsule’ or container for the data and operations.
  • Inheritance: The reuse of base classes (superclasses) to form derived classes (subclasses). Methods and properties defined in the superclass are automatically shared by any subclass.
  • Polymorphism: Same interface, different implementation. The ability to substitute one class for another. This means that different classes may contain the same method names, but the result which is returned by each method will be different as the code behind each method (the implementation) is different in each class.

A class defines (encapsulates) both the properties (data) of an entity and the methods (functions) which may act upon those properties. Neither properties nor methods which can be applied to that entity should exist outside of that class definition.

 

Different Definitions

A method is defined as a self-contained block of code. Each method name must be unique within the application.  This is the name we use when we want to call upon this block of code to perform its job.

Public string DisplayName(string name)

{

                …..

                return name;

}

A little bit of a breakdown:

  • Public is an access modifier, it tells us what parts of the program are going to have access to this method and which one do not.
  • String tells us about the data type of the information the method will return when it is done performing its job. In this case, we can see that the data type of the variable “name” is a string and since that is what we are returning, we have to indicate that here.
  • DisplayName is the name of the method so we know what it is called when we want to use it.
  • The parameter list follows in the parenthesis. Methods do not have to have parameters but they sometimes need outside information brought in so they can perform their job. We can provide any number of parameters needed in a comma separated list.

A class method is defined within the boundaries of a class definition. Each class name “MyClass” must be unique within the application. Each class may contain any number of functions (also known as “methods”), and the function name “DisplayName” must be unique within the class but need not be unique within the application.

Public class MyClass

{

                Public string DisplayName(string name)

                {

                                ….

                                Return name;

                }

}

 

Accessing them is Different

It is important to note that neither a method nor a class can be accessed until an instance of the class object has been created. Calling a method is very straightforward:

String result = DisplayName(“Sara”);

Calling a class method is not so straightforward. First it is necessary to create an instance of the class (an object), then to access the function (method) name through the object. The object name must be unique within the application.

//create an instance of the object

MyClass myObj = new MyClass();

//now I can call a method from inside of MyClass

String result = myObj.DisplayName(“Sara”);

 

State Maintenance can be Tricky

A method by default does not have state: each time that it is called it is treated as a fresh invocation and not a continuation of any previous invocation. An object does have state; each time an object’s method is called it acts upon the object’s state as it was after the previous method call. It is possible for both a regular method and a class method to use local variables, and they both operate in the same way. This means that the local variables do not exist outside the scope of the function or class method, and any values placed in them do not persist between each instance. It is possible for a method to remember values between each time it is called by declaring a variable as static, as in the following example:

Public int Count()

{

                Static int count = 0;

                Count++;

                Return count;

}

 

Each time this method is called it will return a value that is one greater than the previous call. Without the keyword static it would always return the value ’1′. Class variables which need to persist outside of a method are declared at class level, as follows:

Public class Calculator

{

                //define class variables

                Int someValue;

                //setup properties for class variables

                Public int SomeValue

                {

                                Get { return someValue;}

                                Set {somevalue = value;}

                }

                //setup a method that Adds the value

                Public int Add (int number)

{

                                SomeValue += value;

}

//setup a method that Subtracts the value

Public int Subtract (int number)

{

                                SomeValue -= value;

}

}

 

Properties are used to handle the way the data moves in the class when we want to access class-member variables.  Using the Get we can access the value of a variable and through the Set we can change the value of the variable. This helps us control access to the information in the variable and to be able to perform validation and other checks before the value is changed.

 

Conclusion

Many people use the terms “object oriented” or “OO Programming” sometimes out of context.  These terms are slippery and we must be careful that we intend what they truly mean.  There are only three features which really differentiate an Object Oriented language from a non-OO language, and these are Encapsulation, Inheritance and Polymorphism.  If it doesn’t have these three key ideas then it isn’t object-oriented.  OO doesn’t necessarily mean that you will get code that is high reusability and low maintainability that is up to the quality of the code and the experience of the developer, however if you are using object oriented techniques; you are moving in the right direction.

 

Leave a Reply

Your email address will not be published. Required fields are marked *