0% found this document useful (0 votes)
50 views38 pages

Java Classes: Introduction and

The document discusses key concepts related to Java classes including objects, classes, methods, and defining a Java class. It covers topics such as using methods in a Java class, references and aliases, method definitions including arguments and parameters, constructors, and static fields and methods. Examples are provided to illustrate concepts like accessor and mutator methods, defining a class, and resolving identifier conflicts.

Uploaded by

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

Java Classes: Introduction and

The document discusses key concepts related to Java classes including objects, classes, methods, and defining a Java class. It covers topics such as using methods in a Java class, references and aliases, method definitions including arguments and parameters, constructors, and static fields and methods. Examples are provided to illustrate concepts like accessor and mutator methods, defining a class, and resolving identifier conflicts.

Uploaded by

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

Java Classes

Introduction and
Chapter 1

Slides by Steve Armstrong


LeTourneau University
Longview, TX
2007,Prentice Hall
Chapter Contents
• Introduction
• Objects and Classes
• Using the Methods in a Java Class
 References and Aliases
• Defining a Java Class
• Method Definitions
 Arguments and Parameters
 Passing Arguments
 A Definition of the Class Name
 Constructors
 The Method toString
 ...
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Chapter Contents

• . . . Method Definitions – ctd.


 Methods That Call Other Methods
 Methods That Return an Instance of Their Class
 Static Fields and Methods
 Overloading Methods

• Enumeration as a Class
• Packages
 The Java Class Library

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Organizing Our Lives

• For each of the above examples, consider how


the objects are organized

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Organizing Computer Data
• Computer stores/organizes items in similar
manners as the examples
• Ways of organizing data are represented
by Abstract Data Types (ADTs)
• An ADT specifies
 data that is stored
 operations that can be done on the data

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
ADT Terminology
• Data structure: implementation of an ADT
within a programming language
• Collection: an ADT that contains a group
of objects
• Container: a class that implements the
collection
• These last two terms are sometimes used
interchangeably

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Types of ADTs
• Bag
 Unordered collection, may contain duplicates
• List
 A collection that numbers its items
• Stack Match each of these
 Orders items chronologically to the pictures ?

 Last In, First out Click here to return to


pictures
• Queue
 Orders items chronologically
 First in, First out

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Types of ADTs
• Dictionary
 Pairs of items – one is a key
 Can be sorted or not
• Tree
Match each of these
 Arranged in a hierarchy to the pictures ?

• Graph Click here to return to


pictures
 Generalization of a tree

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Objects and Classes 1
• An object is a program construct
 Contains data
 Performs actions
• Objects interact to solve problems
• Actions performed by objects are
defined by methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Objects and Classes
• A class is a kind of object
• A class definition is
a general description of
 what the object is
 what it can do

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Objects and Classes
• All objects in the same class have
 the same kinds of data
 the same methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Using the Methods in a Java Class 2

• Given a class called Name


 Declare a variable
Name joe;
 Create an instance of Name
joe = new Name();
 Alternatively
Name joe = new Name();

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Using the Methods in a Java Class 3

• void methods are used to do a task such


as set the first or last names

• valued methods return a single value

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
References and Aliases 4
• Primitive data types
 byte  float
 short  double
 int  char
 long  boolean

• All other data types are reference or class types


• A reference variable contains address of
(reference to) location in memory of an object

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
References and Aliases
• Consider the results of the code below:

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Defining a Java Class 5
Access
Accessor
orvisibility
visibilitymodifiers
modifiers
Specifies
Specifieswhere
whereaaclass,
class,data
data
field,
field,or
ormethod
methodcan
canbe
beused.
used.

Data
Datamembers
members

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Defining a Java Class
Methods
Methodsthat
thatclasses
classesoften
oftenuse:
use:
••Accessor
Accessor(query)
(query)methods
methods––return
returnvalue
valueof
ofaadata
datafield
field
••Mutator
Mutatormethods
methods––change
changethe
thevalue
valueof
ofaadata
datafield
field

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Method Definitions 7
• General form of method definition

Examples

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Method Definitions 10
public
public void
• Note incorrect, void
setFirst(String
setFirst(String first)
first)
ambiguous use {{
first
first == first;
first;
of identifier first }} //
// end
end setFirst
setFirst
• Solvable by use data object parameter
first
of this first

 this.first refers
to data member public
public void
void
 Note: possible setFirst(String
setFirst(String first)
first)
{{
but not typical this.first
this.first == first;
first;
}} //
// end
end setFirst
setFirst
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Arguments and Parameters 12
• Consider statements:
Name
Name joe
joe == new
new Name();
Name();
joe.setFirst("Joseph");
joe.setFirst("Joseph");
joe.setLast("Brown");
joe.setLast("Brown");

• Arguments/parameters in call match in


number and type to formal parameters in
definition
public
public void
void setFirst(String
setFirst(String firstName)
firstName)
{{
first
first == firstName;
firstName;
}} //
// end
end setFirst
setFirst
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Passing Arguments 13
• When formal parameter is primitive type
 parameter in method initialized by value
 can be constant or variable

public
public void
void setMiddleInitial(char
setMiddleInitial(char middleInitial)
middleInitial)
{{
initial
initial == middleInitial;
middleInitial;
}} //
// end
end setMiddleInitial
setMiddleInitial

joe.setMiddleInitial('Q');
joe.setMiddleInitial('Q');

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Passing Arguments 14
• When a formal parameter has a class type

public
public void
void giveLastNameTo(Name
giveLastNameTo(Name child)
child)
{{
child.setLast(last);
child.setLast(last);
}} //
// end
end giveLastNameTo
giveLastNameTo

 Formal parameter initialized with memory


address of object passed to it.

jamie.giveLastNameTo(jane);
jamie.giveLastNameTo(jane);
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Passing Arguments
• However, a method cannot replace an
object passed to it as an argument

public
public void
void giveLastNameTo2(Name
giveLastNameTo2(Name child)
child)
{{
String
String firstName
firstName == child.getFirst();
child.getFirst();
child
child == new
new Name();
Name();
child.setFirst(firstName);
child.setFirst(firstName);
child.setLast(last);
child.setLast(last);
}} //
// end
end giveLastNameTo2
giveLastNameTo2 child
child isisconsidered
consideredlocal.
local.
ItItwill
willdisappear
disappearwhen
whenthe
the
method
methodfinishes
finishes
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
A Definition of the Class Name 16

• View definition of full class


• Note
 Constructors
 set methods – mutators
 get methods – accessors
 toString method
• Note demonstration program

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Constructors
• Tasks of a constructor
 Allocate memory for object
 Initialize data fields
• Properties
 Same name as class
 No return type (not even void)
 Can have any number of parameters
(including no parameters)
 Note constructors of Name

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Method toString 21
• Note the toString method of class Name
 Returns a string with value of person's name

• For any class, toString method invoked


automatically for command

System.out.println (someObject);

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Methods That Call Other Methods 22

• Note setName method in class Name


 Invokes setFirst and setLast
 setName invokes them without preceding
the method name with object variable and dot
• Consider the getName method
 Calls toString
 Thus both methods always give same result

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Methods That Return an Instance
of Their Class 26
• Consider a different version of setName
public
public Name
Name setName(String
setName(String firstName,
firstName,
String
String lastName)
lastName)
{{
setFirst(firstName);
setFirst(firstName);
setLast(lastName);
setLast(lastName);
return
return this;
this;
}} //
// end
end setName
setName

• The return this; returns a reference


to the invoking object.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Static Fields and Methods 27
• A static data field does not belong to any
one object
 Also called a class variable
 Only one instance of the variable exists for all
instances of the class
• Note that a static data field is not a
constant (final)

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Static Fields and Methods
• All instances of the class reference that
one variable

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Static Fields and Methods
• Consider the need of a method that does
not belong to an object of any type
• Examples
 A method to find the max or min of two or more
numbers
 A square root method

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Static Fields and Methods
• When specified static, a method is still
a member of the class
 However, does not need an object as a prefix
to the call
• Call with the name of the class

int
int maximum
maximum == Math.max(2,
Math.max(2, 3);
3);
double
double root
root == Math.sqrt(4.2);
Math.sqrt(4.2);

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Overloading Methods 29
• Multiple methods within the same class
can have the same name
• Java distinguishes them by noting the
parameters
 Different numbers of parameters
 Different types of parameters
• This is called the signature of the method

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Enumeration as a Class 30
• When an enumeration is defined
 A class is created
 Default methods include
• toString
• equals
• ordinal
• valueOf
• Also possible to provide additional
methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Enumeration as a Class

• Consider the following example of an


enumeration of card suits
• View definition of class Suit
 Note constructor, getColor
• View demonstration program

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Enumeration as a Class 33
• View class LetterGrade
 An enumeration class
 Note class elements
• Consider the following code

LetterGrade myGrade = LetterGrade.B_PLUS;

• Discuss the results of calls to various


LetterGrade methods

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
Packages 34
• Packages enable grouping together
multiple related classes
• Specify a class to be part of a package
with first line
package myStuff;
• Place all classes in same directory which
is named with the name of the package
• In your program which uses the package
import myStuff.*;

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X
The Java Class Library 35
• The Java language has many classes
defined
 Recall the Math class with max and sqrt
• Collection known as

Java Class Library or


Java Application Programming Interface

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X

You might also like