SlideShare a Scribd company logo
4
Most read
8
Most read
Class and Objects
in JAVA
1
What is?
Java is an object-oriented programming (OOP) language. The core
concept of the object-oriented approach is to break complex
problems into smaller objects.
An object is any entity that has a state and behavior. For example,
a bicycle is an object. It has
• States: idle, first gear, etc
• Behaviors: braking, accelerating, etc.
Before we learn about objects, let's first know about classes in Java.
2
Class?
• A class is a blueprint for the object. Before we create an object,
we first need to define the class.
• We can think of the class as a sketch (prototype) of a house. It
contains all the details about the floors, doors, windows, etc.
Based on these descriptions we build the house. House is the
object.
• Since many houses can be made from the same description, we
can create many objects from a class.
3
Create a class in Java
We can create a class in Java using the class keyword. For example,
4
Here, fields (variables) and methods represent the state and behavior of the object respectively.
• fields are used to store data
• methods are used to perform some operations
Create a class in Java
For our bicycle object, we can create the class as
5
In the above example, we have created a class named Bicycle. It contains a field named gear and a
method named braking().
Here, Bicycle is a prototype. Now, we can create any number of bicycles using the prototype. And, all
the bicycles will share the fields and methods of the prototype.
Object?
An object is called an instance of a class.
For example, suppose Bicycle is a class then;
• MountainBicycle.
• SportsBicycle.
• TouringBicycle, etc.
can be considered as objects of the class.
6
Creating an Object in Java
Here is how we can create an object of a class.
7
We have used the new keyword along with the constructor of the class to create an object.
Constructors are similar to methods and have the same name as the class. For example, Bicycle() is
the constructor of the Bicycle class.
Here, sportsBicycle and touringBicycle are the names of objects. We can use them to access fields
and methods of the class.
As you can see, we have created two objects of the class. We can create multiple objects of a single
class in Java.
Access Members of a Class
We can use the name of objects along with the . operator to access members of a class.
For example,
8
Access Members of a Class
In the above example, we have created a class named Bicycle. It includes a field named
gear and a method named braking(). Notice the statement,
9
Here, we have created an object of Bicycle named sportsBicycle. We then use the object to access
the field and method of the class.
sportsBicycle.gear - access the field gear
sportsBicycle.braking() - access the method braking()
Example: Java Class and Objects
10
Output:
Example: Create objects inside the same class
11
Output:
Note that in the previous example, we have created objects inside another class and accessed the members
from that class.
However, we can also create objects inside the same class.
Here, we are creating the object inside the
main() method of the same class.

More Related Content

DOC
Java getstarted
PDF
Lecture2.pdf
DOCX
javaopps concepts
DOCX
Class notes(week 3) on class objects and methods
PDF
Class notes(week 3) on class objects and methods
PPTX
Ch-2ppt.pptx
PPTX
8. objects & classes
PPT
Md02 - Getting Started part-2
Java getstarted
Lecture2.pdf
javaopps concepts
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
Ch-2ppt.pptx
8. objects & classes
Md02 - Getting Started part-2

Similar to Class and Objects in object-oriented programming with Java (20)

PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
PPTX
Android Training (Java Review)
PPT
Java Presentation.ppt
PPTX
Note for Java Programming////////////////
PPT
5 programming-using-java intro-tooop20102011
PPTX
Object_Orientation_Presentation_PPT.pptx
PPTX
Introduction to OOP.pptx
PPTX
Basic concept of Object Oriented Programming
PPT
Lecture java continued
PDF
Java chapter 3 - OOPs concepts
PPT
Unidad o informatica en ingles
PPTX
2 oop
PPTX
classes-objects in oops java-201023154255.pptx
PPTX
Oops concept in Java
PDF
Object Oriented Programming - 5. Class & Object
PPTX
Object Oriended Programming with Java
PPTX
Class and Object in java core programming
PDF
01 Introduction to OOP codinggggggggggggggggggggggggggggggggg
PPTX
PPTX
Object Oriented Programming Tutorial.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
Android Training (Java Review)
Java Presentation.ppt
Note for Java Programming////////////////
5 programming-using-java intro-tooop20102011
Object_Orientation_Presentation_PPT.pptx
Introduction to OOP.pptx
Basic concept of Object Oriented Programming
Lecture java continued
Java chapter 3 - OOPs concepts
Unidad o informatica en ingles
2 oop
classes-objects in oops java-201023154255.pptx
Oops concept in Java
Object Oriented Programming - 5. Class & Object
Object Oriended Programming with Java
Class and Object in java core programming
01 Introduction to OOP codinggggggggggggggggggggggggggggggggg
Object Oriented Programming Tutorial.pptx
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
202450812 BayCHI UCSC-SV 20250812 v17.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Ad

Class and Objects in object-oriented programming with Java

  • 2. What is? Java is an object-oriented programming (OOP) language. The core concept of the object-oriented approach is to break complex problems into smaller objects. An object is any entity that has a state and behavior. For example, a bicycle is an object. It has • States: idle, first gear, etc • Behaviors: braking, accelerating, etc. Before we learn about objects, let's first know about classes in Java. 2
  • 3. Class? • A class is a blueprint for the object. Before we create an object, we first need to define the class. • We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object. • Since many houses can be made from the same description, we can create many objects from a class. 3
  • 4. Create a class in Java We can create a class in Java using the class keyword. For example, 4 Here, fields (variables) and methods represent the state and behavior of the object respectively. • fields are used to store data • methods are used to perform some operations
  • 5. Create a class in Java For our bicycle object, we can create the class as 5 In the above example, we have created a class named Bicycle. It contains a field named gear and a method named braking(). Here, Bicycle is a prototype. Now, we can create any number of bicycles using the prototype. And, all the bicycles will share the fields and methods of the prototype.
  • 6. Object? An object is called an instance of a class. For example, suppose Bicycle is a class then; • MountainBicycle. • SportsBicycle. • TouringBicycle, etc. can be considered as objects of the class. 6
  • 7. Creating an Object in Java Here is how we can create an object of a class. 7 We have used the new keyword along with the constructor of the class to create an object. Constructors are similar to methods and have the same name as the class. For example, Bicycle() is the constructor of the Bicycle class. Here, sportsBicycle and touringBicycle are the names of objects. We can use them to access fields and methods of the class. As you can see, we have created two objects of the class. We can create multiple objects of a single class in Java.
  • 8. Access Members of a Class We can use the name of objects along with the . operator to access members of a class. For example, 8
  • 9. Access Members of a Class In the above example, we have created a class named Bicycle. It includes a field named gear and a method named braking(). Notice the statement, 9 Here, we have created an object of Bicycle named sportsBicycle. We then use the object to access the field and method of the class. sportsBicycle.gear - access the field gear sportsBicycle.braking() - access the method braking()
  • 10. Example: Java Class and Objects 10 Output:
  • 11. Example: Create objects inside the same class 11 Output: Note that in the previous example, we have created objects inside another class and accessed the members from that class. However, we can also create objects inside the same class. Here, we are creating the object inside the main() method of the same class.