40 likes | 132 Views
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
E N D
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
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.
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; }
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); }