Classes and Objects

Class:
  • It is a blue print or template from which object are created.
  • It is encapsulation of data members and methods
  • It is logical entity
  • It is created using class keyword


















Objects:
  • It is instance of class
  • It can access data members and methods of class
  • It is physical entity
  • It is created using new keyword










package com.core.java;

public class Employee {
    int id;
    String name;
    double  salary;
    
    public void display(){
        System.out.println("Id: " + id + ", Name: " +  name + ", Salary: " + salary);
    }
    
    public static void main(String[] args) {
        Employee e1 = new Employee();
        
        e1.id = 111;
        e1.name = "John";
        e1.salary = 15000;
        e1.display();
        
        Employee e2 = new Employee();
        e2.id = 222;
        e2.name = "Tom";
        e2.salary = 10000;
        System.out.println("Id: " + e2.id + ", Name: " +  e2.name + ", Salary: " + e2.salary);
    }
}

No comments:

Post a Comment