1 / 5

Polymorphism

Polymorphism. Head First Java Page 183-188. Polymorphism. Direct result of inheritance - the subclasses inherit the methods that the superclass have. You define a common protocol for a set of classes related through inheritance You’re announcing the rules to any other code.

halia
Download Presentation

Polymorphism

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. Polymorphism Head First Java Page 183-188

  2. Polymorphism • Direct result of inheritance - the subclasses inherit the methods that the superclass have. • You define a common protocol for a set of classes related through inheritance • You’re announcing the rules to any other code. • If you extend me, if you’re a sub-type of me, you can do these things with these methods.

  3. Polymorphism • When you create a superclass for a grouping of classes, any sub-class can be substituted where the super type is expected.???? • Page 184 - Look at how an object is created • Reference variable; create object; link the object & the reference. Object type and reference type are the same • With polymorphism, the reference and the object can be different

  4. Page 186 • With polymorphism, the reference type can be a superclass of the actual object type. • Declare an array of animals – the SuperClass • Because dog, cat, hippo, etc. are extended from Animal, in the array called animal, you can have objects called dog, cat, hippo, etc. • You can loop through the array, and call one of the methods, and every object does the right thing

  5. When you create a superclass for a grouping of classes, any sub-class can be substituted where the super type is expected • Look at Vet class, page 187 • You can have polymorphic arguments and return types • READ page 188 – SUPER imoprtant!!!

More Related