1 / 15

Rules for subclasses

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

farica
Download Presentation

Rules for subclasses

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. 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. Constructors Constructors are never inherited. If no constructor for child is created it will call the default from the parent. If not default is in the parent you will get a compile error.

  3. Inheritance & Polymorphism

  4. Inheritance Person Employee Student GradStudent Undergrad The arrow points to its superclass. The arrow designates the is-a relationship. The Employee is a person The Student is a Person No! A Person may not be a student.

  5. Testing for Inheritance BasketballPlayer CollegeBBPlayer ProBBPlayer • First Test Is – a Ask is this a type of this You can’t cast something to something it is not a type of BasketballPlayeris a CollegeBBPlayer(no) CollegeBBPlayeris a BasketballPlayer(yes)

  6. Testing for Inheritance • Second test is the instanceof • Instanceof checks to see if the object is a type of another object public void processPlayers(BasketballPlayer b) { if(b instanceofProBBPlayer) System.out.println(b.getDescription()); if(b instanceofCollegeBBPlayer) System.out.println(b.getDescription()); }

  7. Polymorphism • Polymorphism: to assign multiple meanings to the same method name getDescription(); toString(); • These methods can refer to objects of their own class or to objects of the classes inherited from their class • Decision is made at run-time called late or dynamic binding

  8. Declaring subclass objects • When an object is created, the reference can refer not only to an object of the superclass, but also to objects of any of its subclasses. BasketballPlayer b = new BasketballPlayer(); BasketballPlayer b2 = new CollegeBBPlayer(); BasketballPlayer b3 = new ProBBPlayer(); This is known as downcasting. It is implicit. There is no need to perform a cast. (Similar to no need to cast an int to a double)

  9. Method is in all classes public String getDescription() { return “College Player”; } public String getDescription() { return "Basketball player"; } BasketballPlayerb1 = new BasketballPlayer("Basketball Player", 0); BasketballPlayerc1 = new CollegeBBPlayer("College player", 0, "Ball"); BasketballPlayerp1 = new ProBBPlayer("Pro Player", 0, 0); What printed? Static or early binding: Compiler checked to see if a getDescription() method was in the BasketballPlayer class. If so it compiled. Dynamic or late binding: runtime checked to see the actual type of object it was and found the method in that class.

  10. Static or Early Binding & Late or Dynamic Binding Static or Early Binding is at Compile time public String getDescription() { return "Basketball player"; } BasketballPlayer b1 = new BasketballPlayer("Basketball Player", 0); BasketballPlayer c1 = new CollegeBBPlayer("College player", 0, "Ball"); BasketballPlayer p1 = new ProBBPlayer("Pro Player", 0, 0); • Compile time checks to make sure the method exist and can be called from the class creating the object • (Left hand side) Late or Dynamic Binding • At Run-Time the decision is made which instance method to call: It looks at the actual type of object that was created. b1.getDescription(); c1.getDescription(); p1.getDescription(); Each object is of the type Basketball at compile time but the subclass at runtime.

  11. Casting • Casting does not change the object referenced; it creates an anonymous reference to that object BasketballPlayer b = new BasketballPlayer(); BasketballPlayer b2 = new CollegeBBPlayer(); BasketballPlayer b3 = new ProBBPlayer(); b is a BasketballPlayer at runtime b2 is still a CollegeBBPlayer at runtime b3 is still a ProBBPlayer at runtime

  12. Polymorphism used for method arguments. This method is only in the BasetballPlayer class. public void showDescription(BasketballPlayer b) { System.out.println(b.getDescription()); } BasketballPlayerb1 = new BasketballPlayer("Basketball Player", 0); BasketballPlayerc1 = new CollegeBBPlayer("College player", 0, "Ball"); BasketballPlayerp1 = new ProBBPlayer("Pro Player", 0, 0); Question: What prints for each? Late or Dynamic binding finds the actual type at runtime and finds the getDescription method in that class.

  13. Explicit Casting (upcasting) • Cast a reference from the subclass upward to the parent • a cast is required. BasekballPlayer b2 = new CollegeBBPlayer(); You would not be able to call any methods that were only in the CollegeBBPlayer class unless you cast the object. b2.getMajor(); // will not compile ((CollegeBBPlayer) b2).getMajor();

  14. Rules for Polymorphic Method call BasketballPlayer c1 = new CollegeBBPlayer("College player", 0, "Ball"); c1.getMajor() // will not compile ((CollegeBBPlayer)c1).getMajor(); //explicit cast At compile time the method must be found in the class BasketballPlayer. If it is not you must do an explicit cast to its actual type. At runtime the actual type is determined and the method is selected from the subclass.

  15. Class Cast Exception error BasketballPlayer b1 = new BasketballPlayer(); BasketballPlayer p1 = new ProBBPlayer(); b1 = (CollegeBBPlayer)p1; java.lang.ClassCastException: ProBBPlayer cannot be cast to CollegeBBPlayer

More Related