Вы находитесь на странице: 1из 3

Lecture 7- Object

March 2, 1999
Jeff Teeters, Instructor
649-7912h / 436-2640w

Merritt College CIS48NG

Oriented programming
http://cis.merritt.edu/~jteeters/java/jc07.rtf

Java Programming
Jeff@Teeters.com

1. Object Oriented Programming: Four principals, (A PIE).


(Abstraction, Polymorphism, Inheritance, Encapsulation) This lecture:
* P PolymorphismFunctions chosen dynamically based on object type
* I Inheritance

New types hierarchically created from existing types

2. Inheritance
Is a relation

One object is a other object. Car is a vehicle. Inheritance.

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)

Subclass can access

private

Subclass cannot access

super.

(example: super.getX() ).

Used to access overloaded superclass method

4. When can Subclass / Superclass be used as each other?


Upcast Be happy:
Can treat subclass object as a superclass object
Downcast-Be sad: Can not treat superclass object as a subcast
public class Circle extends Point { }
Point p1, p2;
// References to point objects
Circle c1, c2;
// References to circle

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--

// The no-Argument Superclass constructor is implicitly called

super( );

// Superclass constructor explicit call. Must be 1st stmt in subclass constructor

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

Cannot be overridden by a subclass

final class

Cannot be inherited. (i.e. cannot be a Superclass).

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);

// Draws box, calling a completely different function

}
}

10. Interface. Similar to class. Starts with keyword interface.


Contains public abstract methods or public static final data (constants).
Used when there is no default implementation to inherit.
Inherited using implements keyword.
Example: public class Circle extends Applet implements ActionListener {
Allows multiple inheritance (multiple is-a relationships).
11. Type-wrapper class for primitive types. Allows treating primitive types as objects, for example call
by reference or dynamic method binding, polymorphism. Byte, Short, Integer, Long, Float, Double. All
inherit from type Number, declared final. Have useful methods. Example: Double.toString().
Assignment: Read Chap 7 of book. Review Summary. Do Self-Review Exercises.
Programming Assignment: (Based on 7.24). Use the above example to help write an applet that draws
random shapes (Ovals and Rectangles). The applet should prompt the user for the number of shapes, then
create an array of Shapes, and initialize each element to either a random Oval or Rectangle. The paint
method should draw all the shapes using a single for loop. Something like: for (int i=0; i<
allShapes.length; i++) allShapes[i].draw(g);

Lecture 7, Page 3 of 3