Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Class And Object

A class is the file generated after compiling your Java file with the use of 'javac' command or in most cases generated automatically by your IDE like Eclipse. In Object Oriented Programming, it will mean something more. Let us look at the Class VS Object concept demonstrated in the below example.

  package com.example.core;

  /**
   * A class is the blue print where objects are derived.
   * An object is an instance of the class. A typical 
   * example is how we build houses in real life. We have 
   * a blue print. Then the construction company uses that 
   * blue print to build multiple houses of exactly the 
   * same specification based on that blue print.
   *
   * @author Rolan Liwanag
   *
   */
  public class ClassAndObject {

    public static void main(String[] args) {
	  ClassAndObject obj = new ClassAndObject();
	  /*
	   * The above code declared a variable named obj of 
	   * type ClassAndObject and by invoking the new 
	   * ClassAndObject() we built an instance of that 
	   * class. ClassAndObject is the class, obj is the 
	   * object.
	   */
	  ClassAndObject anotherObj = new ClassAndObject();
	  /*
	   * Is obj and anotherObj the same object? 
	   * The answer is no!
	   * Is obj and anotherObj of the same type? 
	   * The answer is yes!
	   */
	  System.out.println("obj " + obj);
	  System.out.println("anotherObj " + anotherObj);
	  /*
	   * You will even notice that when we printed out the 
	   * object itself, it tells us that they are different 
	   * objects from each other. Each object were 
	   * instantiated when the 'new' keyword was invoked.
	   */
    }
  }
                            
To elaborate further, we can also say that a Class is the idea of something. For example, we know that a person has 2 hands, 2 feet, normally has 5 fingers on each hand, and 5 fingers on each foot. A person stands up right and has a very advanced form of intelect. When we talk about a person, it is the idea of what a person is. When we address the part, in this case, who the person we are talking about, then, in object oriented programming concept, we are referring to an object. Juan is a person. In OOP, Juan is an instance of the class Person. I hope this tutorial gave you a good grasp of the Class and Object concept.


Back    Next