April 27, 1998CS102-02Lecture 5-1 Object-Oriented Programming Revisited CS 102-02 Lecture 5-1 The...
-
Upload
sybil-powell -
Category
Documents
-
view
217 -
download
0
Transcript of April 27, 1998CS102-02Lecture 5-1 Object-Oriented Programming Revisited CS 102-02 Lecture 5-1 The...
April 27, 1998 CS102-02 Lecture 5-1
Object-Oriented Programming Revisited
CS 102-02
Lecture 5-1
The Wonderful World of Objects
April 27, 1998 CS102-02 Lecture 5-1
A Brief Outline of Today’s Events
• Review of inheritance• protected members
• Subclasses and superclasses
April 27, 1998 CS102-02 Lecture 5-1
Inheritance
• Doesn't mean that classes are born with a silver spoon in their mouths
• The is-a relationship
• The world's objects fall into broad categories– Categories depend on your perspective
April 27, 1998 CS102-02 Lecture 5-1
Broad Means General
Level of detail
Number of matching objects
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
April 27, 1998 CS102-02 Lecture 5-1
A Little Terminology• Component is a direct
subclass of Object• Container is the
direct superclass of Panel
• Container is a superclass of Applet
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
April 27, 1998 CS102-02 Lecture 5-1
The Role of Inheritance
• Found an is-a relationship in the world? Use inheritance because:– Models the world well– Code is more extensible and flexible (For
example, create another Container without worrying about Panel stuff)
• Reuse!
April 27, 1998 CS102-02 Lecture 5-1
Inheriting in Java
• Use extends to indicate the immediate superclass
• A class can only extend one other class (single inheritance)– C++ allows multiple inheritance– Java allows multiple interface inheritance
• Without extends, a class extends java.lang.Object
April 27, 1998 CS102-02 Lecture 5-1
Hiding Information
• Information hiding (a.k.a. encapsulation) is fundamental to OOP– Can't see information you don't need
• Access modifierspublic: everyone can see everything
private: only the class itself can see anything
and the new kid on the block...
April 27, 1998 CS102-02 Lecture 5-1
protecting Your Information
• If you mark an item protected, the item is available to:– The class itself– Other classes in the same package– Subclasses in other packages
April 27, 1998 CS102-02 Lecture 5-1
Accessor RoundupMember Visibility
Accessibleto:
public protected package private
Sameclass Yes Yes Yes Yes
Class insamepackage
Yes Yes Yes No
Subclassin anotherpackage
Yes Yes No No
Not asubclass,anotherpackage
Yes No No No
April 27, 1998 CS102-02 Lecture 5-1
Class Identity Crisis
• Subclass objects are more specific versions of superclass objects– Can always treat a subclass object as a
superclass object– Employee example
A Senior Vice-President object and a Customer Service Representative object are both Employee objects
April 27, 1998 CS102-02 Lecture 5-1
When is Superclass Object Also a Subclass Object?
public class PointData members: protected int x, y;
public class Circle
Data members:
protected double radius;
April 27, 1998 CS102-02 Lecture 5-1
A Class Quiz
• Can we do this?
Point ryersonHall = new Point(12, 15);
Circle buckingham = new Circle();
Circle roundAndRound[] = new Circle[10];
roundAndRound[0] = buckingham;
roundAndRound[1] = ryersonHall;
April 27, 1998 CS102-02 Lecture 5-1
The Test Applet
public class Test extends Applet {private Point pointRef, p; private Circle circleRef, c;
public void init() { p = new Point( 30, 50 );
c = new Circle( 2.7, 120, 89 ); }:
• Points are just (x, y)
• Circles are points with a radius (r)
April 27, 1998 CS102-02 Lecture 5-1
Circles & Points
// Attempt to treat a Circle as a PointpointRef = c; // assign Circle to pointRefg.drawString( "Circle c (via pointRef): " + pointRef.toString(), 25, 70 );
• Assign a subclass reference to a superclass reference– A Circle object is a Point object
• Can't call Circle-specific methods or data with pointRefpointRef.setRadius(9.37); // Compile-time error
Error: Test.java(26): Method setRadius(double) not found in Point
April 27, 1998 CS102-02 Lecture 5-1
Circles & Points
// Treat a Circle as a Circle (with some casting)circleRef = (Circle) pointRef; // cast super to subg.drawString( "Circle c (via circleRef): " +
circleRef.toString(), 25, 100);g.drawString( "Area of c (via circleRef): " +
precision2.format( circleRef.area() ), 25, 115 );
• Convert a superclass reference to a subclass reference– Must use an explicit cast
• Remember that pointRef references a Circle at this point
April 27, 1998 CS102-02 Lecture 5-1
Circles & Points
// Attempt to refer to Point object// with Circle referencecircleRef = (Circle) p; // line 39 in Test.java
• Remember that p is still a Point object
• Java knows that p references a Point, and not a Circle– Throws a CastClassException
April 27, 1998 CS102-02 Lecture 5-1
Building Super Subclasses
• Use super to refer to the parent class
• Circle is-a Point plus a radius– Let the Point class build the Point, and
then Circle can add the radiuspublic Circle( double r, int a, int b ) {
super( a, b ); // Ask Point class to build
// a point
setRadius( r ); // Set the radius ourselves
}
April 27, 1998 CS102-02 Lecture 5-1
Being a super user• Don't have to use super
– If you don’t, the superclass no-argument constructor is called automatically
– If superclass' no-arg constructor isn't defined, it's an error
public Circle() {
// Calls no-arg constructor of Point
setRadius(0.0)
}
April 27, 1998 CS102-02 Lecture 5-1
Using super II
• If you do use super, it's gotta be in the first line of the constructor
public Circle( double r, int a, int b ) {super( a, b ); // Ask Point class to build
// a pointsetRadius( r ); // Set the radius ourselves
}