Tuesday, 21 May 2013

Class and Object EXPLAINED


Class and Object!
Class and Object are the 2 main components of any language following the OOPs principle. Understanding  of these two concepts is very important if you want to become a good programmer.
Objects:
These objects have some STATE and BEHAVIOR.
Let’ s  take Superhero for instance.
Properties like name, planetName, typeOfHero  etc. are states of a Superhero.
Activites or Powers like superHumanStrength, invulnerability, superHumanVision, x_rayVision , heatVision , superHumanBreadth etc . and yes I love Superman.
So, like Superheroes every Object has a state and behavior. State is stored in fields or variables and behavior is shown by methods.
Eg. The method  getSuperHumanStrength()  returns Super Human Strength (Obviously :P)
In software development , methods operate on the internal state of an object and the object to object communication is done via methods.
Classes:
Always remember – “A class is a blueprint from which individual objects are created”
Like what happens when a Architecture makes a blueprint. He makes every building considering the blueprint. So, every building is a instance of that blueprint and they have common properties.
Lets come back to Superheroes
So a class Superhero would look like:





And you can add more methods and variables.
Object and Class relationship:
Any object of a class must have those properties which have been listed by the class or the blueprint which it follows.
Now you can raise a question, “First you said Superhero is an object, then you say it is a class , NOT FAIR !!”
When I was talking about objects, the Superhero I was talking about was Superman or any other superhero that has those properties. Basically an INSTANCE of the class Superhero.
When I say Superhero as a class I mean to say Superhero as a whole or Superhero in general has these properties or the blueprint Superhero.
Hope this helps you understand Classes and Objects better.

EXAMPLE
You must have seen the movie Ironman. So, here's an example related to it.
When Tony is captured he makes a blueprint (which is the class) of his suit which looks like this 

and when he is done building the suit (Object) it looks like 




1 comment: