Object Oriented

The concept of Object-oriented programming is a concept that observe things as real world object. There are many examples of real-world objects: a dog, tv, and your room mates for example. :P

Real-world objects share two characteristics:

  1. State
    State describe what an object is. A dog has state name, breed. TV has state on, off, channel, volume. Your room mate has state name, skin color, height, weight, etc.

  2. Behaviour
  3. Behaviour tell you what an object does or able to perform. Dog have behaviour like barking, wagging tail. TV behaviours like increasing/decreasing volume, turn on, turn off. And your room mate behaviour’s mmm… singing, talking, laughing.

These states and behaviours are closely tied to the object’s real world characteristics and behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods. Hiding internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation.

Benefit of encapsulation:

  • All the data and implementation code will be entirely hidden and prevented from randomly access by other code outside the class. User only need to know the interface, and not the details of internal structure and operation.
  • Ability to modify the code without breaking others who use it, the developer can change or improve any implementation details, as long as the interface doesn’t change.

Let’s have an example:

Po

I would like to create Po (Kungfu Panda) with java. So, what are Panda characteristic?
Panda having state like name, weight, kungfu skill.
Panda behaviour’s are: eating, practicing kungfu.

CREATE CLASS PANDA
class Panda {
      //Panda states stored in fields.
      private String name;
      private Integer weight;
      private Integer kungfuSkill;

      //constructor
      public Panda(String name, Integer weight, Integer kungfuSkill){
         setName(name);
         setWeight(weight);
         setKungfuSkill(kungfuSkill);
      }

      //all interaction are access through methods
      public String getName(){
         return this.name;
      }
      public void setName(String name){
         this.name = name;
      }
      public Integer getWeight(){
         return this.weight;
      }
      public void setWeight(Integer weight){
         this.weight = weight;
      }
      public Integer getKungfuSkill(){
         return this.kungfuSkill;
      }
      public void setKungfuSkill(Integer kungfuSkill){
         this.kungfuSkill = kungfuSkill;
      }

      public void eating(){
         weight = weight + 20;
         System.out.println(name + ” is eating. Weight increase to: ” + weight);
      }
      public void practicing_kungfu(){
         kungfuSkill = kungfuSkill + 100;
         System.out.println(name + ” is practicing. Kungfu skill increase to: ” + kungfuSkill);
      }
}

CREATE PANDA PO
class KungfuPanda {
      public static void main(String[] args) {
         Panda pandaPo = new Panda(“Po”,100,0);
         System.out.println(“New Panda created. His name is: ” + pandaPo.getName());
         System.out.println(“Weight: ” + pandaPo.getWeight());
         System.out.println(“Kungfu skill: ” + pandaPo.getKungfuSkill());

         pandaPo.eating();

         pandaPo.practicing_kungfu();
      }
}

Congratulation!
We just implement object-oriented concept by creating class Panda and object pandaPo.

 

References :

 

3 Responses to Object Oriented

  1. notavailable says:

    Veerrr… bikin Verrr… Panda pake Java n_n

  2. javavera says:

    iyaaaa nggi.. tapi g musti dikasih makan yg byk ya :wink:

  3. jokondo says:

    Object Pandanya bisa banyak neh :D Pembajak film Kungfu Panda, beli yang orri atuh :D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.