Download Programming with Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Programming with Java
1
Chapter 5
Creating Classes
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
2
Objectives
•
•
•
•
•
Understand more thoroughly the characteristics of objectoriented programming.
Know when to appropriately use public and private variables
and methods.
Create your own classes.
Instantiate objects of your class.
Write methods that return values and accept input values.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
3
Creating Classes
•
Applets created in a single class become very large and difficult
to manage.
•
The best way is to break up the tasks into smaller objects with
each responsible for a specific task.
•
Learning to separate the processing from the user interface.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
4
Encapsulation
•
Encapsulation refers to combination of properties and methods
in a single unit.
•
Encapsulation allows an object to maintain the integrity of the
data by “hiding” it from the other objects.
•
Think of an object as a “black box” that carries out a task.
•
The only way to gain access to any of the properties and
methods of an object is to use those properties and methods
specifically declared public.
•
Each object keeps control of its own components, variables,
and methods.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
5
Inheritance
•
You can add enhancements to an existing class without
modifying the original.
•
The existing class is known as base class or superclass.
•
Applet class is the base class and the new class is the
subclass.
•
The Applet class already has the init method; but the init
method you write executes not the one in the Applet class.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
6
Inheritance Continued
•
This is a way to modify the existing method in the base class
with yours in the subclass by giving the same name to the
method.
•
This is called overriding.
•
You can also achieve the affect of deleting a method in the
base class by creating a method in your subclass with same
name and having no statements inside (empty method).
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
7
Interfaces and Inner Classes
•
In Java, you can inherit only one superclass unlike C++.
•
An interface is similar to a class but it exposes a set of
methods.
•
Therefore, you must override all the methods you use and
don’t use.
•
When you implement an interface, you are guaranteeing
that your class will contain all the methods, which are in
the interface.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
8
Interfaces and Inner Classes
Continued
•
Although you can have only one superclass, you can implement
as many interfaces you want.
•
This gives you access to a lot of methods, in addition to the
superclass.
•
You can create an interface the same way you create a class by
replacing the keyword word class with interface.
•
An interface can contain methods and constants but no
variables.
•
An inner class is a class defined within another class.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
9
Abstract Classes
•
These classes are solely written for the purpose of inheritance.
•
It can be a superclass to more than one subclass.
•
An abstract class contains abstract methods that do not contain
any statements, called abstract methods.
•
You cannot instantiate an object from the abstract class. You
can only inherit the abstract class.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
10
Polymorphism
•
Polymorphism refers to more than one method having identical
names.
•
An example of polymorphism is the methods of the Component
class being the superclass are being inherited for both the Label
class and TextField class.
•
For example, the setText and getText methods for work
appropriately for both the Label and TextField classes.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
11
Objects
•
The main idea behind OOP is to create reusable code.
•
An object is a “thing” that characteristics and behaviors.
•
When you instantiate the object from a class the constructor
method in the class executes.
•
You can also declare a button with no label.
•
This illustrates that there is more than one constructor.
•
The constructor with the label executes one constructor method
and the one.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
12
Your Own Objects
•
You can create your own classes and then instantiate multiple
objects in another class.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
13
Creating a New Class
•
•
Your project can have multiple classes and then instantiate these
classes in another class.
Each class can be saved as a separate file with a .java extension
and then store all the files in the same folder.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
14
Public vs. Private
•
As you work with multiple classes the concept of public or
private access becomes more important.
•
Until now you had only one class and everything was public and
it was not critical.
•
The concept of Encapsulation – well designed class keeps its
details hidden form other classes.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
15
Public vs. Private Continued
•
You are using the “get” and “set” methods to gain access.
•
Inside a class, you can declare variables and methods to be
public or private.
•
Public variables and methods can be referenced by an object
instantiated from the class.
•
Only statements within the class itself can reference variables
and methods declared private.
•
Good programming practice is to make all your class variables
private, which can be accessed by only public methods.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
16
Returning Values form a Method
•
So far, we have been coding methods that do not return a value.
•
In this case, void means we are not returning a value.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
17
The Method Header –
General Format
public|private ReturnType MethodName(Arguments)
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
18
The Method Header –
Examples
public float getRate()
public String getDescription()
public float calculateInterest(float fltPrincipal, float fltRate; float fltTime
private void doSomething()
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
19
A return Statement
•
A method that returns a value must contain a return statement.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
20
The return Statement –
General Format
return Value;
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
21
The return Statement Examples
return fltRate;
return “Really big deal”;
return intCount;
return 15;
return fltHours * fltRate
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
22
Passing Arguments to a Method
You can declare a method header that is expecting arguments and pass the
arguments when the method is called.
Parameters
Let us look at an example:
public float calculateExtended(int intQuantity, float fltPrice)
{
return intQuantity * fltPrice;
}
Arguments
fltExtendedPrice = calculateExtended(intNumber, fltUnitPrice);
The arguments and the parameters don’t have the same name and that is okay.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
23
Dividing an Applet Class
•
We will create two classes:
•
The Applet class contains the items and functions that relate
to the user interface, which includes the buttons, labels, and
textfields.
•
The other class will have the variables and the methods to do
the calculation.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
24
Constructors
•
A constructor is a very important method of a class.
•
A constructor is method that is automatically called when a
new object is created from the class.
•
Declaring a class only creates a blueprint for a type of an
object; it does not create a memory location for an actual
object.
•
You allocate memory when you use the new keyword to create
an instance of the class.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
25
Adding a Constructor
•
Create the constructor with the same name as the class without
using the keywords public or private.
•
The constructor must always be public and that is the default.
•
It is not a good idea to have the getText method directly for the
inputs because if there is an empty string, it will throw an
exception.
•
You cannot have the instantiated class display an error message
to the user as all the interaction is done in the applet class.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
26
Obtaining Values from a Private
Class Variables
•
It is always important to even keep the method that calculates
private.
•
You can create a public method containing the prefix get to
retrieve information.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
27
Using a Class Variable for a Total
•
A class variable is a variable that only one copy of that
variable exists, no matter how many times you instantiate the
object.
•
To declare a class variable, you must use the keyword static.
•
If you wish to keep a running total, the class variable will not
be reinitialized to zero each time you instantiate the object.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java
28
Creating a Class For Formatting
•
You will use formatting in probably all your classes, how about
creating class that will take care of all the formatting needs.
•
Formatting a float and double for a different number of decimal
positions the user requires.
•
Formatting for a currency for float and double data types.
•
The class will be called LocalFormat and will be used
throughout the book.
McGraw-Hill/Irwin
© 2002 The McGraw-Hill Companies, Inc. All rights reserved.