Download Java in A Nutshell - LPD

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

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

Document related concepts
no text concepts found
Transcript
Java in a Nutshell
Benoît Garbinato
Tuesday, March 14, 2006
1
Goals of this lesson
To remind you about:
object-oriented programming (OOP)
the Java language & platform
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
2
Object-Oriented Programming
Any object-oriented Programming
language should feature:
encapsulation
inheritance
polymorphism
Java is such an object-oriented
programming language
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
3
Encapsulation (1)
Encapsulation is about distinguishing
specification from implementations
The specification expresses what
all objects of some type are expected to do
An implementation expresses how
some objects of that type are doing it
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
4
Encapsulation (2)
In Java, a class defines both a specification (type)
and an implementation of that specification
In Java, an interface defines a “pure” specification
It it thus impossible to instantiate
(create an instance) 0f an interface
One or more Java classes can
implement a given interface
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
5
Inheritance (1)
Types and subtypes express specification
relationships, i.e., relevant design relationships
Classes and subclasses express implementation
relationships and are irrelevant at the design level
In Java, a class inheritance relationship defines
both a subtype and a subclass relationship
In Java, an interface inheritance relationship is
merely a synonym of subtype relationship
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
6
Inheritance (2)
class inheritance ! subtyping " subclassing
interface inheritance ! subtyping
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
7
Polymorphism (1)
Substitution Principle:
An object of some subtype of type T can be
used wherever an object of type T is required
Vehicle v1 = new Vehicle();
Vehicle v2 = new Car();
Vehicle v3 = new Bicycle();
...
myScreen.drawInColor(v1);
myScreen.drawInColor(v2);
myScreen.drawInColor(v3);
...
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
8
Polymorphism (2)
Polymorphic variables can store objects of varying types
The declared type of a variable is its static type
The type of the object a variable refers is its dynamic type
The Java compiler checks for static-type violations
The Java runtime checks for dynamic-type violations
Car c = new Car();
Vehicle v = new Car();
what is the type
of c and v ?
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
9
Polymorphism (3)
Methods calls are also said to be polymorphic,
meaning that the dynamic type of the variable rather
than its static type determines the method to be called
The method of the subclass is said to override the
method of the superclass
class Vehicle {
void print(){ System.out.println(”I am a vehicle”);}
}
class Bicycle extends Vehicle {}
class Car extends Vehicle {
void print(){ System.out.println(”I am a car”);}
}
Vehicle v1 = new Vehicle();
Vehicle v2 = new Bicycle();
Vehicle v3 = new Car();
v1.print(); v2.print(); v3.print();
what gets printed?
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
10
Polymorphism (4)
Vehicle v1
instance of
Vehicle
void print(){...}
:Vehicle
v1.print();
Vehicle v2
instance of
Bicycle
Vehicle
void print(){...}
:Bicycle
v2.print();
Vehicle v3
instance of
:Car
Car
Vehicle
void print(){...}
void print(){...}
v3.print();
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
11
A simple example (1)
public interface MouseListener {
public void mouseClicked(MouseEvent e);
// Invoked when the mouse has been clicked on a component.
public void mouseEntered(MouseEvent e);
// Invoked when the mouse enters a component.
public void mouseExited(MouseEvent e);
// Invoked when the mouse exits a component.
public void mousePressed(MouseEvent e);
// Invoked when a mouse button has been pressed on a component.
}
public void mouseReleased(MouseEvent e);
// Invoked when a mouse button has been released on a component.
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
12
A simple example (2)
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Spot extends Applet implements MouseListener {
private java.awt.Point clickPoint = null;
private static final int RADIUS = 7;
public void init() {
addMouseListener(this);
}
public void paint(Graphics g) {
g.drawRect(0, 0, getSize().width - 1,
getSize().height - 1);
if (clickPoint != null)
g.fillOval(clickPoint.x - RADIUS,
clickPoint.y - RADIUS,
RADIUS * 2, RADIUS * 2);
}
public void mousePressed(MouseEvent event) {
clickPoint = event.getPoint();
repaint();
}
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
<HTML>
<TITLE>
Spot Applet
</TITLE>
<BODY>
<APPLET
CODE=Spot.class
WIDTH=150
HEIGHT=150>
</APPLET>
</BODY>
</HTML>
}
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
13
Java as programming platform
The Java platform consists of...
The specification of a programming language
The specification of a rich collection of standard
Application Programming Interfaces (APIs)
The specification of a virtual machine (bytecodes)
Various implementations, e.g., one from a Sun
Microsystems (JavaSoft), but also others(IBM, BEA
Virtual Machines, etc.)
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
14
Development process
Java
Virtual
Machine
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
15
Java Development Kit (JDK)
JDK is a compile-time environment that offers:
a reference implementation of the Java language;
a reference implementation of the core Java APIs;
various development tools, e.g., javadoc, javah, etc.
JDK is also a runtime environment that offers:
a reference implementation of the Java virtual machine with:
incremental garbage collection,
green & native threads,
just-in-time compilation,
etc.
Java in a Nutshell © Benoît Garbinato
Tuesday, March 14, 2006
16
Questions?
Tuesday, March 14, 2006
17
Related documents