Friday, September 24, 2010

Object Oriented Programming

I just went to an interview today for a possible deployment position in our company. When asked, "What do you know about Object-Oriented programming?", I answered Inheritance, Abstraction, Encapsulation and Polymorphism easily, but I got lost in the explanation part. It had me thinking that maybe there's an easier way to describe these 4 concepts in a way that could easily be remembered.

OOP is the most natural way of writing your programs given that the language your using supports it. A very good example of this is Java. A programmer using OOP is like a very good writer writing down a very detailed description of his story. A writer writes a detailed description person, place, or the feel of the story. The person, place, or feeling are classes that a programmer can define in detail.

When a writer puts a name on the person, for example, Noel, the writer is now creating an actual representation of the character which is what we call as an object. An object is an instance of a class. Each character have properties like their name, height, weight, hair color, or skin complexion and they can also do certain things like walk, talk, or run. These properties are called attributes while the things that they can do are called methods. If we are to put this in code:

public class Person {
    private String name;
    private String gender;
    private String surname;
    private int height;
    private int weight;

    public void setName(String name) { this.name = name; }

    public String getGender() { return gender; }

    public void setGender(String gender) { this.gender = gender; }

    public String getName() { return name; }

    public void setSurname(String surname) { this.surname = surname; }

    public String getSurname() { return surname; }

    public void setHeight(int height) { this.height = height; }

    public int getHeight() { return height; }

    public void setWeight(int weight) { this.weight = weight; }

    public int getWeight() { return weight; }

    public void say(String message) { System.out.println(message); }

    public void walk() { System.out.println(this.getName() + " is walking"); }

}


Inheritance
The writer continues to tell his story and he is now saying that Noel is a sanitary engineer. The writer describes the sanitary engineer as a person that makes sure that the place is clean. This means that a sanitary engineer inherits what a person is and does while having it's own attributes and methods. Therefore, inheritance is the ability of a class to be derived from another class. The class where it's derived from is called a super-class while the derivative of that is a sub-class. The methods and attributes of the super-class is inherited by the sub-class.

public class SanitaryEngineer extends Person{
    void makeThingsClean(){}
}

Encapsulation
He further describes that there are certain things about a person that people wouldn't know unless they asked like for Noel his surname is Avlas. In OOP, this is called encapsulation. Encapsulation is the hiding of access to certain methods or attributes of a class. This is done by using the following access modifiers: public, protected, and private. (I will discuss the scope of these keywords in another post someday.)

//add these to your Person class
    private String surname;
    public String getSurname(){
        return surname;
    }

Abstraction
In the story Noel uses vacuum cleaner to clean the dust from the corners of the office. When Noel uses the vacuum cleaner, he doesn't really need to know how the vacuum cleaner works but he only needs to know how to use it. Therefore, abstraction is making complex things simple by just exposing only those that are necessary for you to use.

public class VacuumCleaner {
    public void run() {
        System.out.println("this area is being cleane by a vacuum cleaner");
        //some complex operation here
    }
    public void stop() {
        //some complex operation here
        System.out.println("the vacuum cleaner stopped");
    }
}

public class SanitaryEngineer extends Person{
    VacuumCleaner vacuumCleaner = new VacuumCleaner();
    public void makeThingsClean() {
        System.out.println(this.getName() + " is cleaning the area");
        vacuumCleaner.run();
    }
    public void stopCleaning() {
        System.out.println(this.getName() + " stopped cleaning");
        vacuumCleaner.stop();
    }
}


Polymorphism
Finally, the writer says that Noel walks very slowly and that sanitary engineers generally do. Polymorphism is the ability of an object or object's method to appear in many forms and to process objects differently according to their class.

public class SanitaryEngineer extends Person{
    VacuumCleaner vacuumCleaner = new VacuumCleaner();
    public void makeThingsClean() {
        System.out.println(this.getName() + " is cleaning the area");
        vacuumCleaner.run();
    }
    public void stopCleaning() {
        System.out.println(this.getName() + " stopped cleaning");
        vacuumCleaner.stop();
    }
    public void walk(){ System.out.println(this.getName() + " is walking slowly"); }
}


Noel the Sanitary Engineer by The Writer
Noel, is a guy that is about 160cm tall and weighs around 80kgs. He is a sanitary engineer. He is very busy cleaning the office and people around rarely notices him.

One day, Rica, a 120cm tall girl that weighs around 40kgs and is new to the company, was looking for the lavatory when he saw Noel and asked him. Noel was still busy vacuum cleaning the carpet at that time but when Rica asked her, he quickly turned off the vacuum cleaner and showed her where the restroom was. Rica followed Noel as he walks very slowly.

When Rica was done, she introduced herself to noel. From then on, they became very good acquaintances.

-The End

public class NoelTheSanitaryEngineer {
    public static void main(String[] args) {
        SanitaryEngineer noel = new SanitaryEngineer();
        noel.setName("Noel");
        noel.setGender("Guy");
        noel.setHeight(160);
        noel.setWeight(80);
        noel.setSurname("Avlas");
        statePerson(noel);
        Person rica = new Person();
        rica.setName("Rica");
        rica.setGender("Girl");
        rica.setHeight(140);
        rica.setWeight(40);
        rica.setSurname("Adrolle");
        statePerson(rica);
        noel.makeThingsClean();
        rica.say("Where's the restroom?");
        noel.stopCleaning();
        noel.say("Follow me and I'll show you.");
        noel.walk();
        rica.walk();
        rica.say("Thank you!");
        noel.say("You're welcome");
        noel.makeThingsClean();
        System.out.println("After 10 mins...");
        rica.say("By the way I'm " + rica.getName() + " " + rica.getSurname());
        rica.say("Nice to meet you!");
        noel.stopCleaning();
        noel.say("Ow! I'm " + noel.getName() + " " + noel.getSurname());
        noel.say("Nice to meet you too!");
        System.out.println("-The end-");
    }
    public static void statePerson(Person person) {
        System.out.println(person.getName() + " is a " + person.getGender());
        System.out.println(person.getHeight() + "cm tall at "
                + person.getWeight() + "kgs");
    }
}

The output

Noel is a Guy
160cm tall at 80kgs
Rica is a Girl
140cm tall at 40kgs
Noel is cleaning the area
this area is being cleaned by a vacuum cleaner
Rica: Where's the restroom?
Noel stopped cleaning
the vacuum cleaner stopped
Noel: Follow me and I'll show you.
Noel is walking slowly
Rica is walking
Rica: Thank you!
Noel: You're welcome
Noel is cleaning the area
this area is being cleaned by a vacuum cleaner
After 10 mins...
Rica: By the way I'm Rica Adrolle
Rica: Nice to meet you!
Noel stopped cleaning
the vacuum cleaner stopped
Noel: Ow! I'm Noel Avlas
Noel: Nice to meet you too!
-The end-


Question: What do you know about Object-Oriented Programming?

Answer:
Inheritance is the ability of a class to be derived from a super class taking with it it's methods and attributes. Encapsulation is the hiding of implementation of class members. Abstraction is a way of generalization exposing only necessary members to implementing classes. Polymorphism is the ability of an object or method to take many forms while processing each object differently according to their specific class.

Conclusion
This is how I understood OOP. I hope you learned a thing or two from this. There might be some things that are wrong or lacking in this so I suggest you research more about them.

2 comments:

Blackrose said...

Wow! Great post! I enjoyed reading it, so informative :)

Noel Avlas said...

Thank you! Watch out for more posts! :)