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

PPT
Java Presentation.ppt
DOC
C# by Zaheer Abbas Aghani
DOC
C# by Zaheer Abbas Aghani
PPTX
oop 3.pptx
PPTX
Object Oriented Programming Class and Objects
PDF
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
DOCX
javaopps concepts
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
Java Presentation.ppt
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
oop 3.pptx
Object Oriented Programming Class and Objects
Class in Java, Declaring a Class, Declaring a Member in a Class.pdf
javaopps concepts
[OOP - Lec 04,05] Basic Building Blocks of OOP

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

PDF
O6u CS-315A OOP Lecture (1).pdf
PPTX
[OOP - Lec 06] Classes and Objects
PPT
JAVA_INTRODUCTION- History, Constructor, Inheritance
PPTX
Object Oriented Programming_Lecture 2
PPT
07slide.ppt
PPTX
Definning class.pptx unit 3
PPTX
Object Oriented Programming 02b: Classes
PPT
JAVA_INTRODUCTION- History, Constructor, Inheritance
DOCX
Computer Programming 2
PPTX
Java and its concepts
PPTX
Object Oriented Programming - Copy.pptxb
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PPT
Md02 - Getting Started part-2
PDF
principles of proramming language in cppg
PPSX
Java Object Oriented Programming
PPTX
object oriented programming through java basics
PPTX
Basic concept of Object Oriented Programming
PPT
slides 01.ppt
PPTX
IPP-M5-C1-Classes _ Objects python -S2.pptx
PPTX
Classes object(1)
O6u CS-315A OOP Lecture (1).pdf
[OOP - Lec 06] Classes and Objects
JAVA_INTRODUCTION- History, Constructor, Inheritance
Object Oriented Programming_Lecture 2
07slide.ppt
Definning class.pptx unit 3
Object Oriented Programming 02b: Classes
JAVA_INTRODUCTION- History, Constructor, Inheritance
Computer Programming 2
Java and its concepts
Object Oriented Programming - Copy.pptxb
Unit 3-Classes ,Objects and Inheritance.pdf
Md02 - Getting Started part-2
principles of proramming language in cppg
Java Object Oriented Programming
object oriented programming through java basics
Basic concept of Object Oriented Programming
slides 01.ppt
IPP-M5-C1-Classes _ Objects python -S2.pptx
Classes object(1)
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
Complications of Minimal Access Surgery at WLH
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Lesson notes of climatology university.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
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
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Insiders guide to clinical Medicine.pdf
Classroom Observation Tools for Teachers
Complications of Minimal Access Surgery at WLH
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Lesson notes of climatology university.
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Renaissance Architecture: A Journey from Faith to Humanism
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
01-Introduction-to-Information-Management.pdf
Insiders guide to clinical Medicine.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.