Programming Series - 02 - Basics of the Object Oriented Programming

 Hello guys!

I am going to talk about Object Oriented Programming, since it's something that we find in many programming languages and I think that's something any programmer should know.

Frist of all, what is object oriented programming?

It's a set of tools and methods used by software engineers to build reusable and reliable software systems and provides a new view of computation.

Object oriented concepts:

  • Objects (fundamental logical blocks; instances of classes) and Classes (description of a collection of objects that have the same structur and similar behavior)
  • Encapsulation and Inheritance
  • Polymorphism and Dynamic binding
The most important thing in OOP is the four main concepts:
  • Abstraction
  • Encapsulation
  • Hierarchy
  • Polymorphism
Abstraction is the ability of hiding the details of process in order to bring out more clearly other aspects or structures.

Encapsulation is the idea of bundling data and methods that work on that data within one unit. It is often used to hide the internal representation or state of an object from the outside. So, the main benefits are modularity and information-hiding.

Hyerarchy is the ordering of abstraction. Hyierarchy of classes (or inheritance) defines a relation between two classes in which a class uses the structure and behavior defined into another class.
Hierarchy of objects (or aggregation) is a relation in which one object is part of another object.

Polymorphism is the ability of a variable, object or function to take on multiple forms. It allows objects with different internal structures to have a common external interface.

Advantages of OOP:
  • Easy to understand
  • Clear modular structure for programs
  • Objects can be reused in other programs (less development cost)
  • Large programs are easier to write and design with minimum flaws by following OOP concepts
The main concept of the OOP is the class. It have a state and behavior. The state (or attributes) of a class is defined by the variables of that class. The behaviour is implemented with methods, which is a function within that class.
For example, the class Dog have state (ex variable age = 15) and behavior (ex bark). In this situation we can see another concept of OOP, encapsulation. You see that the dog can bark but you don't need to know how that works.

In most languages, like java, methods or variables can be declared as static or non-static. If, for example, in the class dog the age is non-static, you can't find out the age without asking for a specific dog (without instantiating the object). But we can have a static variable, maximum age and you can ask what's the maximum age of dogs, without asking for a specific dog.

I will finish this with a piece of code, for those who are curious. This is how we declare a class dog, in Java:

class Dog  
    {
        static int max_age = 20;
        int age;
        String name;
        void bark(){ System.out.println("Woof!");}
    }

And this is how we can make use of it:

System.out.println(Dog.max_age);
Dog rex = new Dog();
rex.age = 7;
System.out.println(rex.age);
rex.bark();
rex.bark();

The program will output 20 because we can acces the static member and it's 20. 
Then we give Rex an age of 7 and we print that, also.
And then, rex barks two times so it will output:
Woof!
Woof!

I hope you enjoyed reading this and now you have an idea what's Object Oriented Programming about!
Thanks for reading, see you next time! And don't forget to ask me anything!

Post a Comment

0 Comments