|
CSCI A201/A597 and I210
|
| The format is different today. | Who cares. |
| Exactly. Let's keep going. |
That's my Point of view too.
|
We first defined class Point.
A Point has a position (x, y).
class Point {
int x;
int y;
}
These are the features of any Point object: Point.
Pixel is a Point with Color. In Java this is easy to write:
class Pixel extends Point {
Color c;
}
The features of a Pixel are three: int)
int)
Color, call it color
Point and the one new feature that class
Pixel is defining, in other words: That is the resulting blueprint (for![]()
![]()
Pixel)
is a superposition of the two descriptions. That's fine, but set union means that names should be kept distinct.
We'll come back to this in a second.
How do we use Point and Pixel?
Nothing unusual. We use new and expect the blueprints to define the
resulting structures.
Notice that the// somewhere in a method... Point a = new Point(); Pixel b = new Pixel(); a.x = 2; a.y = -10; b.x = 3; b.y = 24; b.color = Color.blue;
Color class is defined in the java.awt package.
Next we asked: have you ever seen a Horse?
The answer was: yes.
Can you describe a Horse?
The answer was: that's actually quite complicated.
OK, so fortunately we know what we're talking about:
class Horse {
// lots of features ...
}
Next we asked: have you ever seen a Unicorn? The answer was: no.
Can you describe a Unicorn though?
Everybody said: yes, that's easy.
class Unicorn extends Horse {
Horn h;
}
So we had the following situation: Horse. Now we said: let's write a play with horses and unicorns.
In our play we could have:
Both h and u are special kinds of binoculars.Horse h = new Horse(); Unicorn u = new Unicorn();
If you put them on you should see the features that their type is defining.
So h.mane and u.mane make sense.
So does u.horn but h.horn doesn't.
For this reason it's not adequate to say:
It is OK, however, to ignore some features, for the sake of being more general.Unicorn g = new Horse();
From our description it follows that all Unicorns are Horses.
That's called polymorphism.
So writing something like this is acceptable:
You can never access theHorse z = new Unicorn();
Horn with z but sometimes you don't
even need that. We could come up with the following similar example:
class Shape {
// two coordinates
}
class Circle extends Shape {
// add a radius
}
class Rectangle extends Shape {
// add a width and a height
}
class Triangle extends Shape {
// add two other points relative to location
}
Why is this useful? If you want to create an array that can store
Circles,
Triangles, and
Rectangles,
Shapes.
I've got room for 100 such shapes (circles, triangles, or rectangles).Shape p[] = new Shape[100];
There's no other way around it, as far as arrays are concerned.
Now we want to explore the name collision problem.
Consider this example:
class Horse {
void neigh() { System.out.println("Horse: Howdy!"); }
}
class Unicorn extends Horse {
void neigh() { System.out.println("Unicorn: Bonjour! "); }
}
Unicorn is listing a feature: neigh.
If Horse had not had it already listed things would've been easy.
But Horses already know how to neigh.
They say: "Howdy!".
So Unicorns redefine the feature
by saying "Hello!" in French.
That's called overriding.
The mechanism is that no matter how you look at a Unicorn,
Unicorn it is, or
Horse it is
Here's the proof:
frilled.cs.indiana.edu%cat Ionesco.java
class Horse {
void neigh() { System.out.println("I am a Horse: Howdy!"); }
}
class Unicorn extends Horse {
void neigh() { System.out.println("I am a Unicorn: Bonjour! "); }
}
class Ionesco {
public static void main(String[] args) {
Unicorn a = new Unicorn();
Horse b = new Unicorn();
a.neigh();
b.neigh();
Horse c = new Horse();
c.neigh();
}
}
frilled.cs.indiana.edu%javac Ionesco.java
frilled.cs.indiana.edu%java Ionesco
I am a Unicorn: Bonjour!
I am a Unicorn: Bonjour!
I am a Horse: Howdy!
frilled.cs.indiana.edu%
That's all we need to know before we go into applets. Here's the basic framework of an applet:
import java.applet.*;
import java.awt.*;
public class Simple extends Applet {
public void paint(Graphics g) {
for (int i = 0; i < 20; i++) {
g.drawString("This is a test.", 20 + 10 * i, 20 + 10 * i);
}
}
}
Here's what you do to install it:
And here it is in action.frilled.cs.indiana.edu%emacs Simple.java frilled.cs.indiana.edu%javac Simple.java frilled.cs.indiana.edu%emacs simple.html frilled.cs.indiana.edu%cat simple.html <html><head><title>Testing a simple applet</title></head><body bgcolor=white> <applet code=Simple width=200 height=200> </applet> </body></html> frilled.cs.indiana.edu%appletviewer simple.html