Академический Документы
Профессиональный Документы
Культура Документы
March 2, 1999
Jeff Teeters, Instructor
649-7912h / 436-2640w
Oriented programming
http://cis.merritt.edu/~jteeters/java/jc07.rtf
Java Programming
Jeff@Teeters.com
2. Inheritance
Is a relation
vs Has a relation
Composition. Car has a tire.. Not inheritance.
Superclass
Super above. Class inheriting from. Example: Vehicle.
Subclass
Subbelow. Inheriting class. Adds to superclass. Example: car.
extends keyword - Used to implement inheritance in Java:
class Circle extends Applet { another example: class Car extends Vehicle {
class object
Superclass of all classes (All classes are subclasses of object).
3. Subclass access to Superclass members
Access modifiers: (Specified in superclass)
public, protected, friendly (package)
private
super.
(example: super.getX() ).
p1 = c1;
// upcast be happy. Its allowed.
c2 = p2;
// downcast be sad. Not allowed. (Unless explicit cast).
c2 = (Circle) p1; // Explicit cast. May still have problems at run-time, if p1 not really a circle
Reason: Subclass has more info than superclass. If downcast, members not defined.
5. When is superclass constructor called when creating a subclass object?
Calls made in subclass constructor:
--None--
super( );
Lecture 7, Page 1 of 3
6. finalize() method called when object garbage collected. (Normally not needed. Garbage collection
automatically frees memory. If superclass, finalize() should be called explicitly from subclass finalize().
7. final methods and classes (final keyword, not same as finalize() method).
final variable Cannot be changed. Recall: final int arraySize = 30;
final method
final class
static and private methods are implicitly also final. (cannot be overridden).
8. abstract vs. concrete classes
concrete class
Can be used to instantiate an object.
abstract class
Cannot be used to instantiate an object (abstract keyword).
iterator class Used to all objects in a container (such as an array). Often abstract.
abstract method Method that must be overridden in a subclass.
9. Polymorphism (Means multiple forms).
Idea: Programs written to manipulate generic classes. Actual objects filled in later.
Recall: Upcast happy (Can assign subclass to reference to superclass). Allows superclass reference
to point to many different subclass object types.
Dynamic method binding: method chosen at run-time according to actual object type.
Example:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
abstract class Shape {
int x, y, siz1, siz2;
// location and size of shape
Color myColor;
// color of object
public void setLoc(int xin, int yin) {x = xin; y = yin; }
public void setMyColor(int r, int g, int b) {myColor = new Color(r,g,b); } // Store color
public abstract void draw(Graphics g); // abstract method to draw shape
public void setSize(int s1, int s2) { siz1 = s1; siz2 = s2; }
}
class Oval extends Shape {
public void draw(Graphics g) {
g.setColor(myColor);
g.fillOval(x, y, siz1, siz2);
}
}
class Rectangle extends Shape {
public void draw(Graphics g) {
g.setColor(myColor);
g.fillRect(x, y, siz1, siz2);
}
}
public class Draw2Figs extends Applet {
public void paint(Graphics g) {
Shape ship;
Oval egg = new Oval();
Rectangle box = new Rectangle();
egg.setLoc(100,100); egg.setSize(20,60); egg.setMyColor(255,0,0); // RGB, Red
box.setLoc(150,200); box.setSize(90,50); box.setMyColor(0,255,0); // RGB, Blue
ship = egg;
ship.draw(g);
// Draws egg
ship = box;
Lecture 7, Page 2 of 3
ship.draw(g);
}
}
Lecture 7, Page 3 of 3