Difference between Object and Class in Java

Java 8Object Oriented ProgrammingProgramming

Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

Class - A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

In the following example, Pummy is a class and myPuppy is an object.

 Live Demo

public class Puppy {
   public Puppy(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
      Puppy myPuppy = new Puppy( "tommy" );
   }
}

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

Output

Passed Name is :tommy
raja
Published on 07-Feb-2018 11:56:35
Advertisements