Tuesday, June 29, 2010

Basics Of Class and Objects

Target audience: Beginners
When I learned java, i could not understand the contract between Class and objects. When i learned more and more,i could get the hang of it.I would like to share my understanding for the people who are beginning to learn about Java class and objects and specially for the people have confusion :)

Definition of the Class is “A template which describes the state(variables) and behavior(methods) of the object of its type.”

Imagine you are going to do business. you are going to manufacture Credit Cards for a client ( e.g: Bank). What will you do first before you start manufacturing? you will design a template or a prototype of a actual Credit Card. you will plan where the owners name should be printed. you will plan where the Credit Card number should be printed. The color ,size of the card and so on. Then you will get a list of user details from your client(E.g: Bank) to create the actual cards. Then you will manufacture thousands or millions of credit cards with actual data printed. You can imagine the ‘Class’ as ‘credit card template’ and the ‘Object’ as the actual credit card printed with user detail (may be with your name on it). The ‘state’ or ‘variables’ are the used detals( name, credit card number and all) That’s it. Classes are the templates of the objects.

//Class, is a template

public class Person {
private String name;
private int hight;

public void Person(String name, int hight) {
this.name = name;
this.hight = hight;

public void sayYourName() {

//Objects, are the actual copies of class

Person me = new Person(“My Name”,185); 
Person you = new Person(“Your Name”,190);

When Java find the “new” key word, then it creates a object or instance(actual copy )of the class and keep it in it memory

Hope it clears your confusion and helps you to understand

No comments:

Post a Comment