1 / 4

AP - Open BasketballPlayer & BasketballPlayerDriver & CollegeBBPlayer

AP - Open BasketballPlayer & BasketballPlayerDriver & CollegeBBPlayer. Rules for subclasses A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods

arien
Download Presentation

AP - Open BasketballPlayer & BasketballPlayerDriver & CollegeBBPlayer

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. AP - Open BasketballPlayer & BasketballPlayerDriver & CollegeBBPlayer Rules for subclasses • A subclass can add new private instance variables • A subclass can add new public, private or static methods • A subclass can override inherited methods • A subclass may not redefine a public method as private • A subclass may not override static methods of the superclass • A subclass should define its own constructors • A subclass cannot directly access the private members of its superclass. It must use accessor or mutators

  2. Method overriding and the super keyword A method in the superclass can override a public method from the parent class. Two ways to do that: Completely rewrite the code Same header and parameter information but different code from parent. Partial overriding Same header an parameter information but code includes a call to the superclass method as well as additional code.

  3. super() with toString() toString() Method from parent class public String toString() { return "Name: " + name + " Number of players: " + count + " Jersey #: " + jerseyNum; } toString() method from subclass public String toString() { return super.toString() + " major: " + major; }

  4. super with methods Parent class method public String computeGrade() { if(myName.equals("")) myGrade = "No grade"; else if (getTestAverage() >= 65) myGrade = "Pass"; else myGrade = "Fail"; return(myGrade); } Parent class method public String computeGrade() { if(myName.equals("")) myGrade = "No grade"; else if (getTestAverage() >= 65) myGrade = "Pass"; else myGrade = "Fail"; return(myGrade); }

More Related