1 / 20

Object-Oriented Programming Revisited

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

rreed
Download Presentation

Object-Oriented Programming Revisited

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming Revisited The Wonderful World of Objects CS 102-02 Lecture 5-1

  2. A Brief Outline of Today’s Events • Review of inheritance • protected members • Subclasses and superclasses

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

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

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

  6. 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!

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

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

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

  10. Accessor Roundup

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

  12. 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;

  13. 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;

  14. 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 ); } :

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

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

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

  18. 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 }

  19. 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) }

  20. 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 }

More Related