200 likes | 223 Views
Understand inheritance, visibility control, and data hiding in object-oriented programming for robust and flexible code. Learn the role of subclasses and superclasses in modeling real-world entities.
E N D
Object-Oriented Programming Revisited The Wonderful World of Objects CS 102-02 Lecture 5-1
A Brief Outline of Today’s Events • Review of inheritance • protected members • Subclasses and superclasses
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
Broad Means General Number of matching objects java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet Level of detail
A Little Terminology java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet • Component is a direct subclass of Object • Container is the direct superclass of Panel • Container is a superclass of Applet
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!
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
Hiding Information • Information hiding (a.k.a. encapsulation) is fundamental to OOP • Can't see information you don't need • Access modifiers public: everyone can see everything private: only the class itself can see anything and the new kid on the block...
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
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
When is Superclass Object Also a Subclass Object? public class Circle Data members: protected double radius; public class Point Data members: protected int x, y;
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;
The Test Applet • Points are just (x, y) • Circles are points with a radius (r) 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 ); } :
Circles & Points • Assign a subclass reference to a superclass reference • A Circle object is a Point object // Attempt to treat a Circle as a Point pointRef = c; // assign Circle to pointRef g.drawString( "Circle c (via pointRef): " + pointRef.toString(), 25, 70 ); • Can't call Circle-specific methods or data with pointRef • pointRef.setRadius(9.37); // Compile-time error • Error: Test.java(26): Method setRadius(double) not found in Point
Circles & Points • Convert a superclass reference to a subclass reference • Must use an explicit cast • Remember that pointRef references a Circle at this point // Treat a Circle as a Circle (with some casting) circleRef = (Circle) pointRef; // cast super to sub g.drawString( "Circle c (via circleRef): " + circleRef.toString(), 25, 100); g.drawString( "Area of c (via circleRef): " + precision2.format( circleRef.area() ), 25, 115 );
Circles & Points • Remember that p is still a Point object • Java knows that p references a Point, and not a Circle • Throws a CastClassException // Attempt to refer to Point object // with Circle reference circleRef = (Circle) p; // line 39 in Test.java
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 radius public Circle( double r, int a, int b ) { super( a, b ); // Ask Point class to build // a point setRadius( r ); // Set the radius ourselves }
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) }
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 point setRadius( r ); // Set the radius ourselves }