Class and Object
What is class ?
• A class is like a blueprint or template used to create objects.
• It defines properties (attributes) and methods (functions).
Example:
1. class Car {
2. String color=‘Black’ ;
3. void drive() {
4. System.out.println("Car is driving");
5. }
6. }
What is Object ?
• An object is an entity that has a state and some behavior.
• State refers to the data or properties and Behavior refers to the actions or
methods
• An object is an instance of a class. An instance is just a real, working object
created from a class.
• Created using a new keyword.
Example:
1. public class Main {
2. public static void main(String[] args) {
3. // Object creation
4. Car C1 = new Car ();
5. // Reading the properties
6. System.out.println (C1.color); // Black
7. }
8. }
Car C1 = new Car ();
Object creation :
Type of object is
car
Variable name of
the object
New keyword tells
java to create a
new object
What type of
object is created ,
-that is car
Example : Blueprint of car.
Properties :
Model name
Color
Price
Functionalities :
Drive
Lock
Unlock
1. class Car {
2. // Properties
3. String model = "Hatchback";
4. String color = "Black";
5. int price = 900000;
6. // Methods
7. void drive() {
8. System.out.println("Zoom Zoom Zoom");
9. }
10. void lock() {
11. System.out.println("Car is now locked");
12. }
13. void unlock() {
14. System.out.println("Car is now unlocked");
15. }
16. }
17. public class Main {
18 . public static void main(String[] args) {
19. // Creating objects
20. Car c1 = new Car();
21. Car c2 = new Car();
22. // Reading the properties
23. System.out.println(c1.color); // Black
24. System.out.println(c1.model); // Hatchback
25. System.out.println(c1.price); // 900000
26. //Reading the methods
27. c1.drive(); //Zoom Zoom Zoom
28. c2.unlock(); // car is now unlocked
29. c1.lock() ; // car is now lock
30. }
31. }
Code:
Class Object
Class is the blueprint of an object. It is used to
create objects.
An object is an instance of the class.
No memory is allocated when a class is
declared.
Memory is allocated as soon as an object is
created.
A class is a group of similar objects.
An object is a real-world entity such as a
book, car, etc.
Class is a logical entity. An object is a physical entity.
A class can only be declared once.
Objects can be created many times as per the
requirement.
An example of class can be a car.
Objects of the class car can be BMW,
Mercedes, Ferrari, etc.
Difference Between Java Classes and Objects
The table below demonstrates the difference between classes and objects in Java:
Thank You

Class and Object in java core programming

  • 1.
  • 2.
    What is class? • A class is like a blueprint or template used to create objects. • It defines properties (attributes) and methods (functions). Example: 1. class Car { 2. String color=‘Black’ ; 3. void drive() { 4. System.out.println("Car is driving"); 5. } 6. }
  • 3.
    What is Object? • An object is an entity that has a state and some behavior. • State refers to the data or properties and Behavior refers to the actions or methods • An object is an instance of a class. An instance is just a real, working object created from a class. • Created using a new keyword. Example: 1. public class Main { 2. public static void main(String[] args) { 3. // Object creation 4. Car C1 = new Car (); 5. // Reading the properties 6. System.out.println (C1.color); // Black 7. } 8. }
  • 4.
    Car C1 =new Car (); Object creation : Type of object is car Variable name of the object New keyword tells java to create a new object What type of object is created , -that is car
  • 5.
    Example : Blueprintof car. Properties : Model name Color Price Functionalities : Drive Lock Unlock
  • 6.
    1. class Car{ 2. // Properties 3. String model = "Hatchback"; 4. String color = "Black"; 5. int price = 900000; 6. // Methods 7. void drive() { 8. System.out.println("Zoom Zoom Zoom"); 9. } 10. void lock() { 11. System.out.println("Car is now locked"); 12. } 13. void unlock() { 14. System.out.println("Car is now unlocked"); 15. } 16. } 17. public class Main { 18 . public static void main(String[] args) { 19. // Creating objects 20. Car c1 = new Car(); 21. Car c2 = new Car(); 22. // Reading the properties 23. System.out.println(c1.color); // Black 24. System.out.println(c1.model); // Hatchback 25. System.out.println(c1.price); // 900000 26. //Reading the methods 27. c1.drive(); //Zoom Zoom Zoom 28. c2.unlock(); // car is now unlocked 29. c1.lock() ; // car is now lock 30. } 31. } Code:
  • 7.
    Class Object Class isthe blueprint of an object. It is used to create objects. An object is an instance of the class. No memory is allocated when a class is declared. Memory is allocated as soon as an object is created. A class is a group of similar objects. An object is a real-world entity such as a book, car, etc. Class is a logical entity. An object is a physical entity. A class can only be declared once. Objects can be created many times as per the requirement. An example of class can be a car. Objects of the class car can be BMW, Mercedes, Ferrari, etc. Difference Between Java Classes and Objects The table below demonstrates the difference between classes and objects in Java:
  • 8.