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)

PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Lesson notes of climatology university.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Trump Administration's workforce development strategy
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Orientation - ARALprogram of Deped to the Parents.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Cell Types and Its function , kingdom of life
Complications of Minimal Access Surgery at WLH
Lesson notes of climatology university.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Abdominal Access Techniques with Prof. Dr. R K Mishra
human mycosis Human fungal infections are called human mycosis..pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
A systematic review of self-coping strategies used by university students to ...
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Trump Administration's workforce development strategy
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
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.