Object Oriented

Class

Java

 

Defining Class

Java 8
class Person {
  
  public String name;
  public int age;
  
  public void greet() {
    System.out.println("Hi! I'm " + name);
  }
  
}

Class is a blueprint from which Objects are created.

Class represents a set of properties (state) and methods (behaviors) that are common to all objects created from it.

 

Instantiate Class

Java 8
Person person1 = new Person();

Person person2 = new Person();

new keyword creates a new object out of class.

Object is giving a reality to a class.

Class is like a blueprint for a house and Object is like an actual house created from that blueprint.