0% found this document useful (0 votes)
3 views13 pages

SH 2 Java Object Oriented Programming

The document discusses the concepts of accessors and mutators in a Rectangle class, detailing how to set and get the width and length attributes. It also explains the creation of objects and the importance of initializing local reference variables before use. Additionally, it provides examples of declaring and initializing reference variables in Java.

Uploaded by

ms8028069
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)
3 views13 pages

SH 2 Java Object Oriented Programming

The document discusses the concepts of accessors and mutators in a Rectangle class, detailing how to set and get the width and length attributes. It also explains the creation of objects and the importance of initializing local reference variables before use. Additionally, it provides examples of declaring and initializing reference variables in Java.

Uploaded by

ms8028069
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

Accessors and Mutators

public class Rectangle Data field, attributes,


{ data members
private double width;
private double length;

public void setWidth(double w)


{ width = w;
}
Setter , Mutator
public void setLength(double len)
{ length = len;
}
public double getWidth()
{ return width;
}
public double getLength() Getter, Accessor
{ return length;
}
public double getArea()
{ return length * width;
}
}
Objects and Classes
This expression creates a
Example: Scanner object in memory.

Scanner Input= new Scanner([Link]);

The object's memory address


is assigned to the Input
variable.

Input Scanner
variable object
Uninitialized Local Reference Variables
• Reference variables can be declared without being initialized.
Rectangle box;
• This statement does not create a Rectangle object, so it is an uninitialized local
reference variable.
• A local reference variable must reference an object before it can be used, otherwise a
compiler error will occur.
box = new Rectangle();

Box Rectangle
variable object
More Examples

You might also like