50 likes | 194 Views
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.
E N D
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. • If you extend me, if you’re a sub-type of me, you can do these things with these methods.
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
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
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!!!