Class VS Object

 Classes in java

  • Classes are used to store our code 
  • Classes in java can be created using class keyword
  • Class is like a blueprint where we can write all of our requirements 

Ex:-  fileName- Demo.java

class Hello{

 public void print(){

System.out.print("It is in print() in Hello class");

}

}


Objects in java

  • Objects are used to store the attributes of a class
  • It is like instance of a class
  • It stores all attributes of a class
  • If we want to access the features of a class we use objects

Ex:- fileName- Demo.java       <Same file>

class Demo {

public static void main (String []args){

Hello h=new Hello();

// 'Hello h 'is a reference of class Hello class

// new Hello() is a instance of a class Hello

// By combining the two we get object it means it stores the all attributes/ features of class Hello 

// .(dot) is used to access the specific features of the class 


h.print();

}

}

Comments

Popular posts from this blog

Constructors in java