A trip to Class

May be for the first time in your life you would be doing something totally different. You can either hate me love me for this. But unfortunately, you have to do it for now.

So, what are you asking me to do for you?
Very simple task, think yourself as an Object.

Huh… again??? Some posts ago also you mentioned the same, but did not hint how to?
Well, this time I will hint. Let’s think ourselves as objects. We have some state like name, qualification, height, weight, complexion and many more. Again we have some behaviors like interacting with others, doing our jobs, building a social reputation, singing, dancing, eating, walking, running, writing, drawing and many more things we do in our practical life.

OK, I am object, so what’s there to do with Class?
Recall from my previous post, with the help of JVM a Class is converted into an object.
With this, we can check for a formal definition of Class.
Class: A class is a code template for creating objects. Class contains state and behaviour in form variables and methods. When an object is created from the class, in terms of OOP it is known as the instance of the class.
In a nutshell, Class is the blueprint while Object is the implementation.

OK so I am an object, where is my class?
Well, the class definition for human object is maintained by almighty GOD. Everything goes in heaven. Someone wrote their ideas of human and put it in a Class definition and someone has compiled and interpreted the same and we are here in this world.
Well, I don’t know those part in detail. When I will meet almighty GOD (I don’t know about the date, time and venue for that), I definitely will have this question to him.

OK, time for a quick class creation with a few state (now on, we’ll be using the term variables) and behaviors (now on, we’ll be using the term methods).

Let’s create our Human class in the world of Java.

Basic Definition of Human Class
If you are aware of UML, you can relate the diagram. But please note, this does not follow any rules.
This is just for learning purpose and may not be very much useful in other aspects.
If you don’t know what UML is, don’t worry. To learn Java, it’s not required at this point.

What this diagram means?
·   You can see the topmost layer is Human, this is the name of the class.

· The second layer has name: String, height: double, complexion: String. These are the variables and their data types. I will discuss variables in next post.

· The third layer has interact, eat, think, sleep, run and walk. These are methods of Human class.
These are the pieces of code for logic to stay. Algorithms get executed here. Actually all the behavior, data gets manipulated in methods. Will detail this part in later posts.

So, in words, our Human will be having name, height and complexion. Our human will interact, eat, think, sleep, run and walk.
That was the idea of Human. Let’s put it into a Java program.

 class Human {  
      String name;  
      double height;  
      String complexion;  
      void eat() {  
      }  
      void interact() {  
      }  
      void sleep() {  
      }  
      void think() {  
      }  
      void run() {  
      }  
      void walk() {  
      }  
 }  

What is this void and other stuffs?
This is Java syntax, we’ll be coming to these portion very soon. For now, just try to relate the variables and methods.

So basically you have defined how your human object will look like and what it will do. Now, the instances of this class will be created by JVM when your application will run on it.

Prev     Next

Palash Kanti Kundu

Palash Kanti Kundu

No comments:

Post a Comment