SlideShare a Scribd company logo
1 of 19
Classes and Objects in Java
1
Basics of Classes in Java
Contents
 Introduce to classes and objects in
Java.
 Understand how some of the OO
concepts learnt so far are supported in
Java.
 Understand important features in Java
classes.
2
Introduction
 Java is a true OO language and therefore
the underlying structure of all Java
programs is classes.
3
Introduction
 A class essentially serves as a template for
an object and behaves like a basic data
type “int”.
 Class provide a convenient method for
packaging a group of logically related data
items and functions(methods) that work on
them.
 It is therefore important to understand how
the fields and methods are defined in a
class and how they are used to build a Java
program that incorporates the basic OO
concepts such as encapsulation, 4
Classes
 A class is a collection of fields (data)
and methods (procedure or function)
that operate on that data.
5
Circle
centre
radius
circumference()
area()
Classes
 The basic syntax for a class definition:
6
class ClassName [extends SuperClassName]
{
[fields declaration]
[methods declaration]
}
Adding Fields: Class Circle with
fields
 Add fields
 The fields (data) are also called the
instance varaibles.
7
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
Adding Methods
 A class with only data fields has no life.
Objects created by such a class cannot
respond to any messages.
 Methods are declared inside the body of the
class
 The general form of a method declaration
is:
8
type MethodName (parameter-list)
{
Method-body;
}
Adding Methods to Class Circle
9
public class Circle {
public double x, y; // centre of the circle
public double r; // radius of circle
//Methods to return circumference and area
public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r;
}
}
Method Body
Data Abstraction
 Declare the Circle class, have created
a new data type – Data Abstraction
 Can define variables of that type:
Circle aCircle;
Circle bCircle;
10
Class of Circle cont.
11
aCircle
Points to nothing (Null Reference)
bCircle
Points to nothing (Null Reference)
null null
Creating objects of a class
 Objects are created dynamically using
the new keyword.
 aCircle and bCircle refer to Circle
objects
12
bCircle = new Circle() ;
aCircle = new Circle() ;
13
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
14
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P
aCircle
Q
bCircle
Before Assignment
P
aCircle
Q
bCircle
After Assignment
Automatic garbage collection
 The object does not have a
reference and cannot be used in
future.
 The object becomes a candidate for
automatic garbage collection.
 Java automatically collects garbage
periodically and releases the memory
used which is to be used in the future.
15
Q
Accessing Object/Circle Data
 Similar to C syntax for accessing data
defined in a structure.
16
Circle aCircle = new Circle();
aCircle.x = 2.0 // initialize center and radius
aCircle.y = 2.0
aCircle.r = 1.0
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
Executing Methods in
Object/Circle
 Using Object Methods:
17
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle
Using Circle Class
// Circle.java: Contains both Circle class and its user class
//Add Circle class code here
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); // invoking method
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
}
18
$>java MyMain
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
Summary
 Classes, objects, and methods are the
basic components used in Java
programming.
 We have discussed:
◦ How to define a class
◦ How to create objects
◦ How to add data fields and methods to classes
◦ How to access data fields and methods to
classes
19

More Related Content

Similar to Unit 1 Part - 2 Class Object.ppt

Similar to Unit 1 Part - 2 Class Object.ppt (20)

09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
 
slides 01.ppt
slides 01.pptslides 01.ppt
slides 01.ppt
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
 
Object & classes
Object & classes Object & classes
Object & classes
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
 
Object and class in java
Object and class in javaObject and class in java
Object and class in java
 
08slide
08slide08slide
08slide
 
JavaYDL8
JavaYDL8JavaYDL8
JavaYDL8
 
L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
06slide
06slide06slide
06slide
 
ch:8CS112
ch:8CS112ch:8CS112
ch:8CS112
 
Java Classes
Java ClassesJava Classes
Java Classes
 
Object oriented database
Object oriented databaseObject oriented database
Object oriented database
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
CS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptxCS1Lesson13-IntroClasses.pptx
CS1Lesson13-IntroClasses.pptx
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 

Recently uploaded

Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesKeppelCorporation
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCRashishs7044
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...lizamodels9
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607dollysharma2066
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024christinemoorman
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCRashishs7044
 
Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst SummitHolger Mueller
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailAriel592675
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCRashishs7044
 
Future Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionFuture Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionMintel Group
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...lizamodels9
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessSeta Wicaksana
 
India Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportIndia Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportMintel Group
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...ictsugar
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Timedelhimodelshub1
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCRashishs7044
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfpollardmorgan
 
Islamabad Escorts | Call 03274100048 | Escort Service in Islamabad
Islamabad Escorts | Call 03274100048 | Escort Service in IslamabadIslamabad Escorts | Call 03274100048 | Escort Service in Islamabad
Islamabad Escorts | Call 03274100048 | Escort Service in IslamabadAyesha Khan
 

Recently uploaded (20)

Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation Slides
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
Call Girls In Connaught Place Delhi ❤️88604**77959_Russian 100% Genuine Escor...
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
 
The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024The CMO Survey - Highlights and Insights Report - Spring 2024
The CMO Survey - Highlights and Insights Report - Spring 2024
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR
 
Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst Summit
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detail
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
 
Future Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted VersionFuture Of Sample Report 2024 | Redacted Version
Future Of Sample Report 2024 | Redacted Version
 
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
Call Girls In Sikandarpur Gurgaon ❤️8860477959_Russian 100% Genuine Escorts I...
 
Organizational Structure Running A Successful Business
Organizational Structure Running A Successful BusinessOrganizational Structure Running A Successful Business
Organizational Structure Running A Successful Business
 
India Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportIndia Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample Report
 
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...Global Scenario On Sustainable  and Resilient Coconut Industry by Dr. Jelfina...
Global Scenario On Sustainable and Resilient Coconut Industry by Dr. Jelfina...
 
Call Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any TimeCall Girls Miyapur 7001305949 all area service COD available Any Time
Call Girls Miyapur 7001305949 all area service COD available Any Time
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdfIntro to BCG's Carbon Emissions Benchmark_vF.pdf
Intro to BCG's Carbon Emissions Benchmark_vF.pdf
 
Islamabad Escorts | Call 03274100048 | Escort Service in Islamabad
Islamabad Escorts | Call 03274100048 | Escort Service in IslamabadIslamabad Escorts | Call 03274100048 | Escort Service in Islamabad
Islamabad Escorts | Call 03274100048 | Escort Service in Islamabad
 

Unit 1 Part - 2 Class Object.ppt

  • 1. Classes and Objects in Java 1 Basics of Classes in Java
  • 2. Contents  Introduce to classes and objects in Java.  Understand how some of the OO concepts learnt so far are supported in Java.  Understand important features in Java classes. 2
  • 3. Introduction  Java is a true OO language and therefore the underlying structure of all Java programs is classes. 3
  • 4. Introduction  A class essentially serves as a template for an object and behaves like a basic data type “int”.  Class provide a convenient method for packaging a group of logically related data items and functions(methods) that work on them.  It is therefore important to understand how the fields and methods are defined in a class and how they are used to build a Java program that incorporates the basic OO concepts such as encapsulation, 4
  • 5. Classes  A class is a collection of fields (data) and methods (procedure or function) that operate on that data. 5 Circle centre radius circumference() area()
  • 6. Classes  The basic syntax for a class definition: 6 class ClassName [extends SuperClassName] { [fields declaration] [methods declaration] }
  • 7. Adding Fields: Class Circle with fields  Add fields  The fields (data) are also called the instance varaibles. 7 public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle }
  • 8. Adding Methods  A class with only data fields has no life. Objects created by such a class cannot respond to any messages.  Methods are declared inside the body of the class  The general form of a method declaration is: 8 type MethodName (parameter-list) { Method-body; }
  • 9. Adding Methods to Class Circle 9 public class Circle { public double x, y; // centre of the circle public double r; // radius of circle //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; } } Method Body
  • 10. Data Abstraction  Declare the Circle class, have created a new data type – Data Abstraction  Can define variables of that type: Circle aCircle; Circle bCircle; 10
  • 11. Class of Circle cont. 11 aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null null
  • 12. Creating objects of a class  Objects are created dynamically using the new keyword.  aCircle and bCircle refer to Circle objects 12 bCircle = new Circle() ; aCircle = new Circle() ;
  • 13. 13 Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle;
  • 14. 14 Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle After Assignment
  • 15. Automatic garbage collection  The object does not have a reference and cannot be used in future.  The object becomes a candidate for automatic garbage collection.  Java automatically collects garbage periodically and releases the memory used which is to be used in the future. 15 Q
  • 16. Accessing Object/Circle Data  Similar to C syntax for accessing data defined in a structure. 16 Circle aCircle = new Circle(); aCircle.x = 2.0 // initialize center and radius aCircle.y = 2.0 aCircle.r = 1.0 ObjectName.VariableName ObjectName.MethodName(parameter-list)
  • 17. Executing Methods in Object/Circle  Using Object Methods: 17 Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle
  • 18. Using Circle Class // Circle.java: Contains both Circle class and its user class //Add Circle class code here class MyMain { public static void main(String args[]) { Circle aCircle; // creating reference aCircle = new Circle(); // creating object aCircle.x = 10; // assigning value to data field aCircle.y = 20; aCircle.r = 5; double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.r+" Area="+area); System.out.println("Radius="+aCircle.r+" Circumference ="+circumf); } } 18 $>java MyMain Radius=5.0 Area=78.5 Radius=5.0 Circumference =31.400000000000002
  • 19. Summary  Classes, objects, and methods are the basic components used in Java programming.  We have discussed: ◦ How to define a class ◦ How to create objects ◦ How to add data fields and methods to classes ◦ How to access data fields and methods to classes 19