SlideShare a Scribd company logo
1
Classes and Objects in Java
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.
3
Introduction
ïź Java is a true OO language and therefore the
underlying structure of all Java programs is classes.
ïź Anything we wish to represent in Java must be
encapsulated in a class that defines the “state” and
“behaviour” of the basic program components known as
objects.
ïź Classes create objects and objects use methods to
communicate between them. They provide a
convenient method for packaging a group of logically
related data items and functions that work on them.
ïź A class essentially serves as a template for an object
and behaves like a basic data type “int”. 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, inheritance, and polymorphism.
4
Classes
ïź A class is a collection of fields (data) and
methods (procedure or function) that
operate on that data.
Circle
centre
radius
circumference()
area()
5
Classes
ïź A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
ïź The basic syntax for a class definition:
ïź Bare bone class – no fields, no methods
public class Circle {
// my circle class
}
class ClassName [extends
SuperClassName]
{
[fields declaration]
[methods declaration]
}
6
Adding Fields: Class Circle with fields
ïź Add fields
ïź The fields (data) are also called the
instance varaibles.
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
7
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 but immediately after the declaration of
data fields.
ïź The general form of a method declaration is:
type MethodName (parameter-list)
{
Method-body;
}
8
Adding Methods to Class Circle
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
9
Data Abstraction
ïź Declare the Circle class, have created a
new data type – Data Abstraction
ïź Can define variables (objects) of that
type:
Circle aCircle;
Circle bCircle;
10
Class of Circle cont.
ïź aCircle, bCircle simply refers to a Circle
object, not an object itself.
aCircle
Points to nothing (Null Reference)
bCircle
Points to nothing (Null Reference)
null null
11
Creating objects of a class
ïź Objects are created dynamically using
the new keyword.
ïź aCircle and bCircle refer to Circle objects
bCircle = new Circle() ;aCircle = new Circle() ;
12
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
13
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P
aCircle
Q
bCircle
Before Assignment
P
aCircle
Q
bCircle
Before Assignment
14
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 to be used in the future.
Q
15
Accessing Object/Circle Data
ïź Similar to C syntax for accessing data
defined in a structure.
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)
16
Executing Methods in
Object/Circle
ïź Using Object Methods:
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle
17
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);
}
}
[raj@mundroo]%: java MyMain
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
18
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

More Related Content

PPTX
C# classes objects
PPTX
Chapter 07 inheritance
PPTX
JAVA AWT
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPT
Introduction to method overloading & method overriding in java hdm
PPTX
Abstract class in c++
PPTX
Classes, objects in JAVA
PPT
Method overriding
C# classes objects
Chapter 07 inheritance
JAVA AWT
Basic Concepts of OOPs (Object Oriented Programming in Java)
Introduction to method overloading & method overriding in java hdm
Abstract class in c++
Classes, objects in JAVA
Method overriding

What's hot (20)

PDF
Wrapper classes
PPT
Inheritance in java
PPTX
Classes and Objects in C#
PPTX
Exception handling
PPT
Java static keyword
PPT
Inheritance
PPTX
Interface in java
PPTX
Inheritance in java
PPTX
Ppt on this and super keyword
PPT
Final keyword in java
PPTX
Encapsulation C++
PPT
Core java concepts
 
PPTX
Virtual base class
PPTX
Pointers, virtual function and polymorphism
PPTX
Java Method, Static Block
PPTX
Method overloading
PDF
Php array
PPTX
Object Oriented Programming with C#
PPT
Collection Framework in java
PPTX
Java Annotations
Wrapper classes
Inheritance in java
Classes and Objects in C#
Exception handling
Java static keyword
Inheritance
Interface in java
Inheritance in java
Ppt on this and super keyword
Final keyword in java
Encapsulation C++
Core java concepts
 
Virtual base class
Pointers, virtual function and polymorphism
Java Method, Static Block
Method overloading
Php array
Object Oriented Programming with C#
Collection Framework in java
Java Annotations
Ad

Similar to Lect 1-class and object (20)

PPT
Unit 1 Part - 2 Class Object.ppt
PPT
Java sem i
PPTX
Class and Object.pptx
 
PPT
Java Presentation.ppt
PPT
Lecture 2 classes i
PPT
Core Java unit no. 1 object and class ppt
PPTX
Java Classes
PPT
Module 3 Class and Object.ppt
PPT
packages and interfaces
PDF
Lecture-03 _Java Classes_from FAST-NUCES
PPT
Chapter 9 Objects and Classes JAVA learning
PPT
09_ Objects and Classes in programming.ppt
PPT
08slide
PPT
JavaYDL8
PPTX
PPTX
PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
PPT
slides 01.ppt
PPT
inheritance of java...basics of java in ppt
PPT
Object and class
Unit 1 Part - 2 Class Object.ppt
Java sem i
Class and Object.pptx
 
Java Presentation.ppt
Lecture 2 classes i
Core Java unit no. 1 object and class ppt
Java Classes
Module 3 Class and Object.ppt
packages and interfaces
Lecture-03 _Java Classes_from FAST-NUCES
Chapter 9 Objects and Classes JAVA learning
09_ Objects and Classes in programming.ppt
08slide
JavaYDL8
Unit 1 Part - 3 constructor Overloading Static.ppt
slides 01.ppt
inheritance of java...basics of java in ppt
Object and class
Ad

More from Fajar Baskoro (20)

PPTX
Pengembangan Basis Data untuk Web Application.pptx
PPTX
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
PPTX
Sosialisasi Program Digital Skills Unicef 2025.pptx
PDF
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
PDF
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
PDF
Pemrograman Mobile menggunakan kotlin2.pdf
PPTX
Membangun Kewirausahan Sosial Program Double Track.pptx
PPTX
Membangun Kemandirian DTMandiri-2025.pptx
PDF
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
PDF
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
PPTX
Seleksi Penerimaan Murid Baru 2025.pptx
PPTX
Pengembangan Program Dual Track 2025-2.pptx
PPTX
Pengembangan Program Dual Track 2025-1.pptx
PDF
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
PPTX
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PPTX
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
PPTX
Program Dual Track Kalimantan Timur 2025.pptx
PDF
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
PPTX
Pengembangan Program Digital Skills - 2025.pptx
PPTX
PPT-Proyek Magang Kewirausahaan Double Track.pptx
Pengembangan Basis Data untuk Web Application.pptx
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Sosialisasi Program Digital Skills Unicef 2025.pptx
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Pemrograman Mobile menggunakan kotlin2.pdf
Membangun Kewirausahan Sosial Program Double Track.pptx
Membangun Kemandirian DTMandiri-2025.pptx
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
Seleksi Penerimaan Murid Baru 2025.pptx
Pengembangan Program Dual Track 2025-2.pptx
Pengembangan Program Dual Track 2025-1.pptx
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
Program Dual Track Kalimantan Timur 2025.pptx
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Pengembangan Program Digital Skills - 2025.pptx
PPT-Proyek Magang Kewirausahaan Double Track.pptx

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Essential Infomation Tech presentation.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Introduction to Artificial Intelligence
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Transform Your Business with a Software ERP System
PDF
Understanding Forklifts - TECH EHS Solution
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
System and Network Administraation Chapter 3
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
How to Migrate SBCGlobal Email to Yahoo Easily
Wondershare Filmora 15 Crack With Activation Key [2025
Essential Infomation Tech presentation.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Operating system designcfffgfgggggggvggggggggg
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Introduction to Artificial Intelligence
CHAPTER 2 - PM Management and IT Context
Transform Your Business with a Software ERP System
Understanding Forklifts - TECH EHS Solution
wealthsignaloriginal-com-DS-text-... (1).pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
System and Network Administraation Chapter 3
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Reimagine Home Health with the Power of Agentic AI​
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Design an Analysis of Algorithms I-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025

Lect 1-class and object

  • 1. 1 Classes and Objects in Java Basics of Classes in Java
  • 2. 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.
  • 3. 3 Introduction ïź Java is a true OO language and therefore the underlying structure of all Java programs is classes. ïź Anything we wish to represent in Java must be encapsulated in a class that defines the “state” and “behaviour” of the basic program components known as objects. ïź Classes create objects and objects use methods to communicate between them. They provide a convenient method for packaging a group of logically related data items and functions that work on them. ïź A class essentially serves as a template for an object and behaves like a basic data type “int”. 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, inheritance, and polymorphism.
  • 4. 4 Classes ïź A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area()
  • 5. 5 Classes ïź A class is a collection of fields (data) and methods (procedure or function) that operate on that data. ïź The basic syntax for a class definition: ïź Bare bone class – no fields, no methods public class Circle { // my circle class } class ClassName [extends SuperClassName] { [fields declaration] [methods declaration] }
  • 6. 6 Adding Fields: Class Circle with fields ïź Add fields ïź The fields (data) are also called the instance varaibles. public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle }
  • 7. 7 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 but immediately after the declaration of data fields. ïź The general form of a method declaration is: type MethodName (parameter-list) { Method-body; }
  • 8. 8 Adding Methods to Class Circle 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
  • 9. 9 Data Abstraction ïź Declare the Circle class, have created a new data type – Data Abstraction ïź Can define variables (objects) of that type: Circle aCircle; Circle bCircle;
  • 10. 10 Class of Circle cont. ïź aCircle, bCircle simply refers to a Circle object, not an object itself. aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null null
  • 11. 11 Creating objects of a class ïź Objects are created dynamically using the new keyword. ïź aCircle and bCircle refer to Circle objects bCircle = new Circle() ;aCircle = new Circle() ;
  • 12. 12 Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle;
  • 13. 13 Creating objects of a class aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle Before Assignment
  • 14. 14 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 to be used in the future. Q
  • 15. 15 Accessing Object/Circle Data ïź Similar to C syntax for accessing data defined in a structure. 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)
  • 16. 16 Executing Methods in Object/Circle ïź Using Object Methods: Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle
  • 17. 17 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); } } [raj@mundroo]%: java MyMain Radius=5.0 Area=78.5 Radius=5.0 Circumference =31.400000000000002
  • 18. 18 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