Class vs object in java

  1. Class 
    • Blueprint for object
    • A Programming construct which defines  properties and behaviors for Object.
    • In java everything is encapsulated under classes.
    • use keyword “class” to initial class
  2. Object
    • Instance of class
    • any real life entity which has properties and behaviors
    • use keyword  “new” to create object

Example for Class:

public class Dog {
	// properties
	int numberLeg;
	int eye;
//	 behaviors 
//  dog can walk
	 void walk(){
		 System.out.println("Animal Walk");
	 }
//	 dog can run
	 void run(){
		 System.out.println("Animal Run !");
	 }
//	 it cave more properties and behaviors
}

Example of Object:

public class TestClass {

	public static void main(String[] args) {
//	 instance object dog 
		Dog d = new Dog();
		d.walk();
		d.run();

	}

}

Output:

Animal Walk
Animal Run !

 

Add a Comment

Your email address will not be published. Required fields are marked *