View Sidebar
Object oriented programming

Object oriented programming

January 28, 2014 8:09 PM0 comments

Lately I’ve seen a lot of people struggling with this concept and being the very base of modern programming I think I should talk about it.
OOP, formally called Object Oriented Programming it’s a paradigm that helps the programmer to express himself in a more natural way with concepts he already knows from daily life. This also dramatically increases the level of understanding of the code written, maintainability and further code addition.

Basically you have classes with methods, properties and member variables to designate objects. This principle is called encapsulation. Methods are functions through which the user interacts with the object. Note the difference between properties and member variables. Though it’s language dependent, a property often designates a variable with GET and SET (mutator and accessor functions) and does not have memory allocated on its own however the associated filed does and a member variable or field designates a class variable. Look at the example below:

using System;
class Student
{
 
   private string code = "N.A";
   private string name = "not known";
   private int age = 0;
 
   // Declare a Code property of type string:
   public string Code
   {
      get
      {
         return code;
      }
      set
      {
         code = value;
      }
   }
   
   // Declare a Name property of type string:
   public string Name
   {
      get
      {
         return name;
      }
      set
      {
         name = value;
      }
   }
 
   // Declare a Age property of type int:
   public int Age
   {
      get
      {
         return age;
      }
      set
      {
         age = value;
      }
   }
   public override string ToString()
   {
      return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
   }
 
   public static void Main()
   {
      // Create a new Student object:
      Student s = new Student();//here is called a constructor
            
      // Setting code, name and the age of the student
      s.Code = "001";
      s.Name = "Zara";
      s.Age = 9;
      Console.WriteLine("Student Info: {0}", s);
      //let us increase age
      s.Age += 1;
      Console.WriteLine("Student Info: {0}", s);
      Console.ReadKey();
    }
}

We also have at least two special functions(methods) in a class, those are the constructor and destructor, their role is to initialize the object(allocate memory in non managed languages – C++ and members initialization) and to destroy the object(free allocated memory in non managed languages, not really used in managed languages) respectively. The compiler provides an empty constructor and destructor by default, they don’t anything. Pay attention that a constructor has the same name as the class and it does not return anything. The destructor has a “~” in front and has the same name as the class. The destructor makes no sense in managed languages because the garbage collector is in charge of when an object is destroyed.

Another principle of OOP is security. Often we use access modifiers to accomplish this (private, protected, public and internal in C#).

  • public – can be accessed by anyone in or outside the class.
  • protected – can be accessed by anyone in the same class or in a class that inherits the containing class.
  • private – can be accessed by anyone in the same class.
  • internal (C# specific) – can be accessed only from the same assembly, but not other assemblies. Java has this by default in packaging.

As I already stated abstraction was the main reason of OOP, because a more natural, faster and easier way to code was needed. Programmers needed to avoid “spaghetti code” to make their programs easier to understand and easier to maintain. I will talk about inheritance a bit later as it is a vast subject and it’s language dependent(C++ allows native multiple inheritance while C# and Java require Interface to accomplish this).

Will talk about polymorphism, abstract classes, overloading and templates on a future article.
Finally some of the OOP languages out there are: C++ (C is not), PHP, Java, C#, Ruby, Python etc.

If you are anxious and want to learn faster you can find a good resource here

Leave a reply


Simple Share Buttons