exampracticeonline.com
Object and Class

Objects and Class in Java

Classes and Objects are fundamental concepts and building blocks of Object Oriented Programming which revolve around the real life entities.

What is Class ?

A class is a user defined blueprint or prototype from which objects are created.  It represents the set of properties or methods that are common to all objects of one type.  In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

The following SampleCycle class is a sample of a class :

class SampleCycle {

    int speed = 0;
    int gear = 1;

    
    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println(" speed:" + 
             speed + " gear:" + gear);
    }
}

 The fields speed, and gear represent the object's state, and the methods (changeGear, speedUp etc.) define its interaction with the outside world.

 

What is Object ?

It is a basic unit of Object Oriented Programming and represents the real life entities. Every entity in this real world is Object. All these objects have a state, behavior and Identity.

  1. State : It is represented by attributes of an object. It also reflects the properties of an object.
  2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
  3. Identity : It gives a unique name to an object and enables one object to interact with other objects.

Example of an object : Cat

Cat have it attributes/state like hieght,color,specie and behaviour/methods like meow,running,sleeping.

Creating an Object

In java a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects.

There are three steps when creating an object from a class −

  • Declaration − A variable declaration with a variable name with an object type.

  • Instantiation − 'new' keyword is used to create the object.

  • Initialization − 'new' keyword is followed by a call to a constructor.

Following is an example of creating an object −

Example

public class Sample {

   public Sample(String name) {
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myPuppy
      Sample sample= new Sample( "Vishnu" );
   }
}

If we compile and run the above program, then it will produce the following result −

Output

Passed Name is :vishnu

 

Accessing Instance Variables and Methods

To access an Instance variables and methods we create objects of the class. To access an instance variable, following is the fully qualified path −

/* We First create an object */
ObjectReference = new Constructor();

/* Now we call a variable as follows */
ObjectReference.variableName;

/* Now we you can call a class method as follows */
ObjectReference.MethodName();

Example

This example explains how to access instance variables and methods of a class.

public class Sample {

   int SampleAge;

   public Sample(String name) {
      // This constructor is a parameterized constructor
      System.out.println("Name is :" + name );
   }

   public void setAge( int age ) {
      SampleAge = age;
   }

   public int getAge( ) {
      System.out.println("Sample age is :" + SampleAge );
      return SampleAge;
   }

   public static void main(String []args) {
      /* Now we create Object */
      Sample mySample = new Sample( "Vishnu" );

      /* Call class method to set age */
      mySample.setAge( 2 );

      /* Call another class method to get Sample age */
      mySample.getAge( );

      /* You can access instance variable as follows as well */
      System.out.println("Variable Value :" + mySample.SampleAge );
   }
}

If we compile and run the above program, then it will produce the following result −

Output

Name chosen is :Sample
Sample age is :2
Variable Value :2