0% found this document useful (0 votes)
43 views35 pages

UNIT - 4 Classes, Methods & Objects in Java: Prof. Pradnya N. Sadigale (Assistant Prof - Lohegaon

The document discusses key concepts of object-oriented programming in Java including classes, objects, methods, constructors, modifiers, instance and class variables and methods, and arrays of objects. It provides examples to demonstrate creating Circle objects, using constructors, private variables with accessor methods, static variables and methods, passing objects to methods, and calculating total area of an array of Circle objects. The document explains concepts like abstraction, encapsulation, inheritance and polymorphism which are fundamental to object-oriented design.

Uploaded by

pradnya sadigale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views35 pages

UNIT - 4 Classes, Methods & Objects in Java: Prof. Pradnya N. Sadigale (Assistant Prof - Lohegaon

The document discusses key concepts of object-oriented programming in Java including classes, objects, methods, constructors, modifiers, instance and class variables and methods, and arrays of objects. It provides examples to demonstrate creating Circle objects, using constructors, private variables with accessor methods, static variables and methods, passing objects to methods, and calculating total area of an array of Circle objects. The document explains concepts like abstraction, encapsulation, inheritance and polymorphism which are fundamental to object-oriented design.

Uploaded by

pradnya sadigale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIT_4

Classes, Methods &


Objects in Java
Prof. Pradnya N. Sadigale
(Assistant Prof.Lohegaon)
Chapter 4 Objects and Classes
• OO Programming Concepts
• Creating Objects and Object Reference Variables
• Differences between primitive data type and object type
• Automatic garbage collection
• Constructors
• Modifiers (public, private and static)
• Instance and Class Variables and Methods
• Scope of Variables
• Use the this Keyword
• Case Studies (Mortgage class and Count class)
OO Programming Concepts

An object A Circle object

Data Field
data field 1
radius = 5

... State
Method
data field n findArea

method 1

... Behavior

method n
Class and Objects

Circle UML Graphical notation for classes

radius: double UML Graphical notation for fields

UML Graphical notation for methods


findArea(): double

new Circle() new Circle()

circle1: Circle circlen: Circle UML Graphical notation


for objects
radius = 2 ... radius = 5
Class Declaration
class Circle {
double radius = 1.0;

double findArea(){
return radius * radius * 3.14159;
}
}
Declaring Object Reference Variables
ClassName objectReference;

Example:
Circle myCircle;
Creating Objects
objectReference = new ClassName();

Example:
myCircle = new Circle();

The object reference is assigned to the object reference variable.


Declaring/Creating Objects
in a Single Step
ClassName objectReference = new ClassName();

Example:
Circle myCircle = new Circle();
Differences between variables of
primitive Data types and object types

Primitive type int i = 1 i 1

Object type Circle c c reference

c: Circle
Created using
new Circle() radius = 1
Copying Variables of Primitive
Data Types and Object Types
Primitive type assignment Object type assignment
i=j c1 = c2

Before: After: Before: After:

i 1 i 2 c1 c1

j 2 j 2 c2 c2

c1: Circle c2: Circle

radius = 5 radius = 9
Garbage Collection
As shown in the previous figure,
after the assignment statement c1 =
c2, c1 points to the same object
referenced by c2. The object
previously referenced by c1 is no
longer useful. This object is known
as garbage. Garbage is
automatically collected by JVM.
Garbage Collection, cont
TIP: If you know that an object is no
longer needed, you can explicitly
assign null to a reference variable
for the object. The Java VM will
automatically collect the space if the
object is not referenced by any
variable.
Accessing Objects
• Referencing the object’s data:
objectReference.data
myCircle.radius

• Invoking the object’s method:


objectReference.method
myCircle.findArea()
Example 4.1 Using Objects

• Objective: Demonstrate creating


objects, accessing data, and using
methods.

TestCircle Run
Constructors
Circle(double r) {
radius = r;
}
Constructors are a
Circle() { special kind of
radius = 1.0;
} methods that are
invoked to construct
myCircle = new Circle(5.0);
objects.
Constructors, cont.
A constructor with no parameters is referred to
as a default constructor.
 Constructors must have the same name
as the class itself.
 Constructors do not have a return type—
not even void.
 Constructors are invoked using the new
operator when an object is created.
Constructors play the role of initializing
objects.
Example 4.2 Using Classes from
the Java Library
• Objective: Demonstrate using classes from the
Java library. Use the JFrame class in the
javax.swing package to create two frames;
use the methods in the JFrame class to set
the title, size and location of the frames and
to display the frames.

TestFrame Run
Example 4.3 Using Constructors

• Objective: Demonstrate the role of


constructors and use them to create
objects.

TestCircleWithConstructors Run
Visibility Modifiers and
Accessor Methods
By default, the class, variable, or data can be
accessed by any class in the same package.
 public
The class, data, or method is visible to any class in any
package.

 private
The data or methods can be accessed only by the declaring
class.
The get and set methods are used to read and modify private
properties.
Example 4.4
Using the private Modifier
and Accessor Methods
In this example, private data are used for the
radius and the accessor methods getRadius and
setRadius are provided for the clients to retrieve
and modify the radius.

TestCircleWithAccessors Run
Passing Objects to Methods
• Passing by value (the value is the reference to the object)
Example 6.5 Passing Objects as Arguments

TestPassingObject Run
Passing Objects to Methods, cont.
main printAreas
method method
times

n 5 5 Pass by value (here the value is 5)

myCircle Reference Reference Pass by value (here the value is the


reference for the object)

myCircle: Circle

radius = 1
Instance
Variables, and Methods

Instance variables belong to a specific instance.

Instance methods are invoked by an instance of


the class.
Class Variables, Constants,
and Methods
Class variables are shared by all the instances of the
class.

Class methods are not tied to a specific object.


Class constants are final variables shared by all the
instances of the class.
Class Variables, Constants,
and Methods, cont.

To declare class variables, constants, and methods,


use the static modifier.
Class Variables, Constants,
and Methods, cont.
UML Notation: Memory
+: public variables or methods
-: private variables or methods
underline: static variables or metods

circle1:Circle 1 radius

instantiate -radius = 1
radius is an instance CircleWithStaticVariable -numOfObjects = 2
variable, and
numOfObjects is a -radius
class variable -numOfObjects
2 numOfObjects
+getRadius(): double instantiate
+setRadius(radius: double): void circle2:Circle
+getNumOfObjects(): int
+findArea(): double
-radius = 5 5 radius
-numOfObjects = 2
Example 4.6
Using Instance and Class Variables
and Method
Objective: Demonstrate the roles of
instance and class variables and their
uses. This example adds a class variable
numOfObjects to track the number of
Circle objects created.

TestCircleWithStaticVariable Run
Scope of Variables
• The scope of instance and class variables is the
entire class. They can be declared anywhere
inside a class.
• The scope of a local variable starts from its
declaration and continues to the end of the block
that contains the variable. A local variable must
be declared before it can be used.
The Keyword this
• Use this to refer to the current object.
• Use this to invoke other constructors of the object.
Array of Objects
Circle[] circleArray = new Circle[10];

An array of objects is actually an


array of reference variables. So
invoking circleArray[1].findArea()
involves two levels of referencing as
shown in the next figure. circleArray
references to the entire array.
circleArray[1] references to a Circle
object.
Array of Objects, cont.
Circle[] circleArray = new Circle[10];

circleArray reference circleArray[0] Circle object 0


circleArray[1]

… Circle object 1

circleArray[9] Circle object 9


Array of Objects, cont.
Example 4.7: Summarizing the areas
of the circles

TotalArea Run
Class Abstraction
Class abstraction means to separate class implementation from the
use of the class. The creator of the class provides a description of the
class and let the user know how the class can be used. The user of the
class does not need to know how the class is implemented. The detail
of implementation is encapsulated and hidden from the user.
Example 4.8 The Mortgage
Class
Mortgage

-annualInterestRate: double
-numOfYears: int
-loanAmount: double
Mortgage
+Mortgage()
+Mortgage(annualInterestRate: double,
numOfYears: int, loanAmount: double)
+getAnnualInterestRate(): double TestMortgageClass
+getNumOfYears(): int
+getLoanAmount(): double
+setAnnualInterestRate(annualInteresteRate: double): void
+setNumOfYears(numOfYears: int): void Run
+setLoanAmount(loanAmount: double): void
+monthlyPayment(): double
+totalPayment(): double
Example 4.9 The Count Class

TestVoteCandidate Run

You might also like