0% found this document useful (0 votes)
12 views

Classes and Object in Java

Uploaded by

Janani Selvaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Classes and Object in Java

Uploaded by

Janani Selvaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Dr.

SNS Rajalakhsmi College of Arts and Science


(An Autonomous Co-Education Institution)
Coimbatore – 641049
www.drsnsrcas.ac.in

CLASSES AND OBJECT IN JAVA

Presentation by
Mrs.S.Janani
Assistant Professor
Department of Information Technology
[email protected]
Department of

IT
Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 1
INTRODUCTION
• Object-oriented programming (OOP) involves programming using
objects. An object represents an entity in the real world that can be
distinctly identified. For example, a student, a desk, a circle, a button,
and even a loan can all be viewed as objects.

• The state of an object (also known as its properties or attributes) is


represented by data fields with their current values. A circle object, for
example, has a data field radius, which is the property that
characterizes a circle. A rectangle object has the data fields width and
height, which are the properties that characterize a rectangle .

• The behavior of an object (also known as its actions) is defined by


methods. To invoke a method on an object is to ask the object to perform
an action. For example, you may define methods named getArea() and
getPerimeter() for circle objects. A circle object may invoke getArea() to
return its area and getPerimeter() to return its perimeter.
Department of
www.snsgroup.com
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 2
Objects of the same type are defined using a common class. A class is a
template, blueprint, or contract that defines what an object’s data
fields and methods will be. An object is an instance of a class. You can
create many instances of a class. Creating an instance is referred to as
instantiation.

The terms object and instance are often interchangeable. The


relationship between classes and objects is analogous to that between
an apple-pie recipe and apple pies: You can make as many apple pies as
you want from a single recipe.

Department of
www.snsgroup.com
IT
Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 3
OBJECTS
An object has both a state and behavior. The state defines the object, and
the behavior defines what the object does.

Class Name: Circle A class template

Data Fields:
radius is _______

Methods:
getArea

Circle Object 1 Circle Object 2 Circle Object 3 Three objects of


the Circle class
Data Fields: Data Fields: Data Fields:
radius is 10 radius is 25 radius is 125

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 4
CLASSES
Classes are constructs that define objects of the same type. A
Java class uses variables to define data fields and methods to
define behaviors. Additionally, a class provides a special type of
methods, known as constructors, which are invoked to construct
objects from the class.

The Circle class is different from all of the other classes you
have seen thus far. It does not have a main method and
therefore cannot be run; it is merely a definition for circle objects.
The class that contains the main method will be referred to in
this book, for convenience, as the main class.

The illustration of class templates and objects in can be


standardized using Unified Modeling Language (UML) notation.
This notation is called a UML class diagram, or simply a class
diagram.
Department of
www.snsgroup.com
IT Information
Technology
Drsnsrcas/CPreprocessor/April 27, 2024 Slide No : 5
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field

/** Construct a circle object */


Circle() {
}
Constructors
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}

/** Return the area of this circle */


double getArea() { Method
return radius * radius * 3.14159;
}
}
using circle classes

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No : 7
Example: A Bank Account Object

public BankAccount(long accountNumber)


public void deposit(double amount)
public void withdraw(double amount)
public double getBalance()
public void transfer(double amount,
BankAccount targetAccount)

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :8
EXAMPLE

Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :9
Department of
www.snsgroup.com
IT Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024
Slide No :10
Department of
www.snsgroup.com
IT
Information
Technology Drsnsrcas/CPreprocessor/April 27, 2024

You might also like