Lesson 5 A First Look at Classes
Lesson 5 A First Look at Classes
Chapter Topics
• Objects and Classes
• Writing a Simple Class, Step by Step
• Instance Fields and Methods
• Constructors
• Passing Objects as Arguments
• Overloading Methods and Constructors
• Scope of Instance Fields
• Packages and import Statements
6.1 Objects and Classes (1 of 5)
Example:
6.1 Objects and Classes (4 of 5)
Example:
6.1 Objects and Classes (5 of 5)
Class Fields
Member methods
…
• The public access specifier in “public class Rectangle” indicates that the class will be
publicly available to code that is written outside the Rectangle.java file. Almost
all of the classes that we write in this book are public.
• By using the private access modifier, a class can hide its data from code
outside the class to protect the data from accidental corruption.
• A class usually has private fields, and public methods that access those fields.
Access Specifiers
• An access specifier is a Java keyword that indicates how a field or method can be
accessed.
• public
– When the public access specifier is applied to a class member, the member
can be accessed by code inside the class or outside.
• private
– When the private access specifier is applied to a class member, the member
cannot be accessed by code outside the class. The member can be accessed
only by methods that are members of the same class.
Header for the setLength Method
Creating a Rectangle Object
• This dynamically calculates the value of the rectangle’s area when the
method is called.
• Now, any change to the length or width variables will not leave the area of
the rectangle stale.
UML Data Type and Parameter Notation
• Fields and methods that are declared as previously shown are called
instance fields and instance methods.
• Objects created from a class each have their own copy of instance fields.
• Instance methods are methods that are not declared with a special keyword,
static.
Instance Fields and Methods (2 of 2)
• Constructors have a few special properties that set them apart from normal
methods.
– Constructors have the same name as the class.
– Constructors have no return type (not even void).
– Constructors may not return any values.
– Constructors are typically public.
Constructor for Rectangle Class
• Two or more methods in a class may have the same name as long as their
parameter lists are different.
• When this occurs, it is called method overloading. This also applies to
constructors.
• Method overloading is important because sometimes you need several
different ways to perform the same operation.
Overloaded Method add
Method Signature and Binding
• A method signature consists of the method’s name and the
data types of the method’s parameters, in the order that they
appear. The return type is not part of the signature.
Package Description
java.applet Provides the classes necessary to create an applet.
java.awt Provides classes for the Abstract Windowing Toolkit. These classes are used in
drawing images and creating graphical user interfaces.
java.io Provides classes that perform various types of input and output.
java.lang Provides general classes for the Java language. This package is
automatically imported.
java.net Provides classes for network communications.
java.security Provides classes that implement security features.
java.sql Provides classes for accessing databases using structured query language.
java.text Provides various classes for formatting text.
java.util Provides various utility classes.
javax.swing Provides classes for creating graphical user interfaces.
Exercise
/** /**
The Circle class stores data about a circle The getRadius method returns the circle's radius.
for the Circle Class programming challenge. @return The radius.
*/ */
public class Circle
{ public double getRadius()
private final double PI = 3.14159; // Constant for pi {
private double radius; // The circle's radius return radius;
}
/** /**
This constructor initializes the object with The area method returns the circle's area.
a radius of 0.0. @return The area of the circle.
*/ */
/** /**
The constructor initializes the object with The diameter method returns the circle's diameter.
a specified radius. @return The diamter of the circle.
@param r The circle's radius. */
*/
public double diameter()
public Circle(double r) {
{ return radius * 2;
radius = r; }
} /**
The circumference method returns the
/** circle's circumference.
The setRadius method sets the circle's radius. @return The circumference of the circle.
@param r The radius. */
*/ public double circumference()
public void setRadius(double r) {
{ return 2 * PI * radius;
radius = r; }
} }
Demo Class import java.util.Scanner;
/**
This program demonstrates a solution to the
Circle Class programming challenge.
*/