SlideShare a Scribd company logo
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

More Related Content

PPTX
OOP in C# Classes and Objects.
DOCX
OOP and C++Classes
PPTX
classes object fgfhdfgfdgfgfgfgfdoop.pptx
PDF
Class And Object- Java Object Oriented Programming Lab Report
PDF
Lecture2.pdf
PDF
Object Oriented Programming - 5. Class & Object
PPTX
c#(loops,arrays)
PPTX
Java constructors
OOP in C# Classes and Objects.
OOP and C++Classes
classes object fgfhdfgfdgfgfgfgfdoop.pptx
Class And Object- Java Object Oriented Programming Lab Report
Lecture2.pdf
Object Oriented Programming - 5. Class & Object
c#(loops,arrays)
Java constructors

Similar to Class and Object in java core programming (20)

PPTX
Python oop class 1
PPTX
object oriented programming_Classes_Objects.pptx
PPTX
14. Java defining classes
PPTX
Java constructors
PPTX
Classes and Objects In C# With Example
PPTX
Class and Object.pptx
DOCX
C# Unit 2 notes
PPT
Unidad o informatica en ingles
PPTX
Oop2010 Scala Presentation Stal
PDF
Design patterns in javascript
PDF
Unit 2 notes.pdf
PPTX
Object Oriented Concept
PPT
Object Oriented Programming with Java
PDF
Session 3 - Object oriented programming with Objective-C (part 1)
PPTX
Object Oriended Programming with Java
PPTX
Constructor & destructor
PPT
Class methods
PPT
creating objects and Class methods
PDF
Goal The goal of this assignment is to help students understand the.pdf
PPT
Oop lec 4(oop design, style, characteristics)
Python oop class 1
object oriented programming_Classes_Objects.pptx
14. Java defining classes
Java constructors
Classes and Objects In C# With Example
Class and Object.pptx
C# Unit 2 notes
Unidad o informatica en ingles
Oop2010 Scala Presentation Stal
Design patterns in javascript
Unit 2 notes.pdf
Object Oriented Concept
Object Oriented Programming with Java
Session 3 - Object oriented programming with Objective-C (part 1)
Object Oriended Programming with Java
Constructor & destructor
Class methods
creating objects and Class methods
Goal The goal of this assignment is to help students understand the.pdf
Oop lec 4(oop design, style, characteristics)
Ad

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
master seminar digital applications in india
PPTX
Institutional Correction lecture only . . .
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
Chinmaya Tiranga quiz Grand Finale.pdf
master seminar digital applications in india
Institutional Correction lecture only . . .
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
A systematic review of self-coping strategies used by university students to ...
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
O5-L3 Freight Transport Ops (International) V1.pdf
Microbial disease of the cardiovascular and lymphatic systems
2.FourierTransform-ShortQuestionswithAnswers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Types and Its function , kingdom of life
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
Ad

Class and Object in java core programming

  • 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 : Blueprint of 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 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: