Professional Documents
Culture Documents
Java
Java
B.Ramamurthy
Copyright 1998
CSE Department
08/12/23 BR 1
Topics for Discussion
OO Design Principles
Java Virtual Machine
Java Application Structure
Class and objects
Methods and Variables
Access/Visibility Modifiers
Debugging
Inheritance
Abstract classes, interfaces and implementation
JDK Event Model
Applets and Appletviewer
Summary
08/12/23 BR 2
Object-Oriented Principles
OOP
08/12/23 BR 3
Conventional Compiled
Languages
Mac
Mac Compiler
Compiler Mac Hardware
C++source
08/12/23 BR 4
Java Virtual Machine
Java compiler :javac JVM
JVM
C++source Byte code PC Interpreter PC Hardware
JVM
08/12/23 BR 5
“Run-anywhere” Capability
Package of related
(JAVA API)
classes : java.awt
java.util Random
Date Dictionary
08/12/23 BR 7
Java API : A Simplistic
View
API
packages
classes
08/12/23 BR 9
Types of Programs
Java is fully object-oriented.
Every “function” has to be attached to a class.
You will mainly deal with three types of programs:
class: methods and data (variables +
constants) describing a collection (type) of
object
application: class that has a main method: represents
a our regular program
applet: class that is meant for execution using a
appletviewer/browser
08/12/23 BR 10
Problem Solving Using
Java
OO Design and Progamming in Java
08/12/23 BR 11
What is an Object?
08/12/23 BR 12
Classes are
Blueprints
A class defines the general nature of a collection
of objects of the same type.
The process creating an object from a class is
called instantiation.
Every object is an instance of a particular class.
There can be many instances of objects from
the same class possible with different values for
data.
08/12/23 BR 13
Example
objects
Object
References
redRose
class Rose
blueRose
class
08/12/23 BR 14
Instantiation :
Examples
class FordCar ---- defines a class name FordCar
FordCar windstar; ---- defines a Object reference windStar
windstar = new FordCar(); ---- instantiates a windstar
Object
08/12/23 BR 15
Operator new and “dot”
08/12/23 BR 16
Elements of a Class
class
header body
modifiers, statements
parameters variables,
type, name constants
class
variables
constants
methods
08/12/23 BR 18
Defining Classes
Syntax:
class class_name {
data-declarations
constructors
methods }
Constructors are special methods used for
instantiating (or creating) objects from a class.
Data declarations are implemented using variable
and constant declarations.
08/12/23 BR 19
Naming Convention
08/12/23 BR 20
A complete example
08/12/23 BR 21
Identify Objects
08/12/23 BR 22
The Random class
08/12/23 BR 23
Design
An instance of Random
An instance of string number generator
Input and fill up message. Generate Random integer
Attach (concatenate)
08/12/23 BR 25
Class Components
08/12/23 BR 26
Method Invocation/Call
Syntax:
method_name (values);
object_name.method_name(values);
classname.method_name(values);
Examples:
computeSum(); // call to method from within the class
where it is located
YourRose.paintIt(Red);
Math.abs(X);
08/12/23 BR 27
Defining Methods
A method is group of (related) statements
that carry out a specified function.
A method is associated with a particular
class and it specifies a behavior or
functionality of the class.
A method definition specifies the code to
be executed when the method is
invoked/activated/called.
08/12/23 BR 28
Method Definition : Syntax
08/12/23 BR 29
Return Type
08/12/23 BR 30
Return Statement
08/12/23 BR 31
Parameter List
Parameter list specified in method header provides a
mechanism for sending information to a method.
It is powerful mechanism for specializing an object.
The parameter list that appears in the header of a
method
specifies the type and name of each parameter and
is called formal parameter list.
The corresponding parameter list in the method
invocation is called an actual parameter list.
08/12/23 BR 32
Parameter list : Syntax
Formal parameter list: This is like molds or
templates
(parm_type parm_name, parm_type parm_name, ....)
Actual parameter list: This is like material that fit
into the mold or template specified in the formal
list:
(expression, expression....)
08/12/23 BR 33
Method Definition : review
definition
header
body
Visibility
modifiers
parameter list
return type Name
{ statements }
08/12/23 BR 34
Method Definition :
Example
08/12/23 BR 35
...Example (contd.)
public int Perimeter (int Length, int Width)
{
int Temp; // local temporary variable
Temp = 2 * (Length + Width); // compute
perimeter
return Temp; // return computed value
}
08/12/23 BR 36
What happens when a
method is called?
08/12/23 BR 37
Method Invocation :
semantics
Operating
System 1 1. OS to main method
2 2. Main method execution
Main method
8
Rect.Area(….) 3. Invoke Area
3
4. Transfer control to Area
7
4 8 5. Execute Area method
6. Return control back to
main method
Area 7. Resume executing main
5 method 6 8. Exit to OS
08/12/23 BR 38
Constructors
A Constructor is used to create or instantiate an
object from the class.
Constructor is a special method:
It has the same name as the class.
It has no return type or return statement.
Typically a class has more than one constructor:
a default constructor which has no parameters,
and other constructors with parameters.
08/12/23 BR 39
Constructors (contd.)
You don’t have to define a constructor if you need only
a default constructor.
When you want initializing constructors :
1. you must include a default constructor in this case.
2. You will use initializing constructors when you want the
object to start with a specific initial state rather than as
default state.
3. Example: Car myCar(Red); // initializing constructor for
Car class with color as parameter
08/12/23 BR 40
Visibility Modifiers
08/12/23 BR 42
CLASSPATH
08/12/23 BR 43
CLASSPATH
08/12/23 BR 44
Inheritance
08/12/23 BR 46
Representing the
Relationship
BankClass
has a has a
has a
Account [ ] MortgageSVC BrokerageSVC
is a is a
Checking Savings
is a : use inheritance
has a : use composition, or membership
08/12/23 BR 47
Modifers
08/12/23 BR 48
Example : Words
Main class
Book class
Super class
Uses
is a
Dictionary Class
subclass
08/12/23 BR 49
Example : School
Student
Main class
uses
Grad Student
08/12/23 BR 50
Abstract Class (ABC) and
methods
An abstract class or method is defined using “abstract”
modifier.
An abstract method has no definition in the super class.
ABC cannot be instantiated.
Sole purpose of ABC is to provide an appropriate super
class from which classed may inherit.
Classes from which objects can be instantiated are
called concrete classes.
08/12/23 BR 51
Example
subclasses
08/12/23 BR 52
Interface
An abstract method is one that does not have a
definition within the class. Only the prototype is present.
An interface is collection of constants and abstract
methods.
Syntax
interface interface_name {
constant -declarations;
abstract methods;
}
08/12/23 BR 53
Example
interface EPA {
bool emissionControl();
bool pollutionControl();
…
}
class NYepa implements EPA {
bool emissionControl () {
details/semantics /statements how to implement it }
class CAepa implements EPA {
bool emissionControl () {….
// different details on implementation….}
08/12/23 BR 54
Inheritance and Interfaces
08/12/23 BR 55
Types of Programs
Console programs:
Control-driven: You start the program and the
program code determines the sequence of events.
Mostly the actions are predetermined.
Windows-based programs:
Event-driven: The operation of the program
depends on what you do with the controls presented
(usually by the GUI)
Selecting menu items, pressing buttons, dialog
interaction cause actions within the program.
08/12/23 BR 56
Events
JAVA API
Events
08/12/23 BR 59
Event Handling Process
08/12/23 BR 60
Java Event Model
Java event model has changed quite significantly
between JDK1.0 and JDK 1.1.
Many Web Browsers still use JDK1.0.
JDK1.0 event model is good for understanding event-
driven programming and for simple applications but
does not scale well for large applications.
It is impossible to take a in-depth look at any one.
But we will learn enough to use some basic features.
08/12/23 BR 61
Java Class Hierarchy
java.lang Object
java.awt
Component
“others”
java.applet package
Window Panel
Applet
Frame Dialog
08/12/23 BR 62
JDK1.1 Event Model :
System support
08/12/23 BR 63
JDK1.1 Event Model :
Application Support
08/12/23 BR 64
Implementing Listeners
An application (class) becomes a listener when it
implements the listener interface.
Java 1.1 event model provides a variety of listeners.
EX:
If an application is interested in button click, it will
have to implement ActionListener interface.
actionPerformed() method of the ActionListener will
have be implemented by the application.
When the button click event occurs, actionPerformed()
method will be called with event object as argument.
08/12/23 BR 65
Example: Doodle.java
1. Include “implements” at the header of the class : This
application uses MouseListener and
MouseMotionListener interfaces
2. Invoke “ methods” to add/activate required listeners.
Here we have three:
a) mouse click
b) mouse motion
c) window close
3. Set up board : use paint() and fillRect() methods.
08/12/23 BR 66
Example (contd.)
08/12/23 BR 67
Applet
08/12/23 BR 68
Requirements
html
file
Applet javac includes .html
file appletviewer
.java
Java runtime
JVM
Applet
file
.class
08/12/23 BR 71
Example 1: Using only
paint()
//file : MyApplet.java
import java.applet.Applet; // step 1
import java.awt.Graphics;
public class MyApplet extends Applet //step 2
{
public void paint( Graphics g) // step 3
{ g.drawString(“To climb a ladder”, 20, 90); }
}
08/12/23 BR 72
Example 1: interpretation
08/12/23 BR 73
html file
08/12/23 BR 74
Example 1: execution
semantics
08/12/23 BR 75
More on Applets
08/12/23 BR 76
On-line Information
file:/util/lang/jdk1.1.6/api/API_users_guide.html
has user’s manual for Java API
/util/lang/jdk1.1.6/demo
has many demo programs
and of course, Sun’s home page has the JDK
distribution.
08/12/23 BR 77
Summary
We studied Java classes and objects, object-
oriented programming, applications and applets.
A good understanding of the event-model is
necessary to write event-driven programs.
Graphics and awt features (buttons, panels etc.)
will tremendously enhance the capabilities and
presentation of the event driven programs.
08/12/23 BR 78