10Polymorphism

10Polymorphism - CSE 114 Computer Science I Polymorphism...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Red Bay, Labrador CSE 114 – Computer Science I Polymorphism
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Inheritance Overview Inheritance allows you to define a very general class then later define more specialized classes by adding new detail the general class is called the base or parent class The specialized class inherits all the properties of the general class specialized classes are derived from the base class they are called derived or child classes After the general class is developed, you only have to write the "difference" or "specialization" code for each derived class A class hierarchy: classes can be derived from derived classes (child classes can be parent classes) any class higher in the hierarchy is an ancestor class any class lower in the hierarchy is a descendent class
Background image of page 2
Overriding Verses Overloading Overriding Same method name Same signature One method in ancestor, one in descendant Overloading Same method name Different signature Both methods can be in same class
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Polymorphism with methods/operators Polymorphism: two or more methods or operators with the same name but different definitions A polymorphic method or operator is said to be "overloaded" Examples: + (addition and concatenation) multiple constructors Example: public void setUpValue(int setValue) upValue = setValue; } public void setUpValue() { upValue = 1; }
Background image of page 4
Polymorphism – with classes Polymorphic Object – an object variable that can refer to different types of objects at different points in time Polymorphic references in Java can be created in 2 ways Using inheritance Using interfaces
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Derived Classes: a Class Hierarchy Person Student Employee Faculty Staff Undergraduate Graduate MastersDegree NonDegree PhD
Background image of page 6
Apparent vs. Actual Type Each constructed object has an apparent type and an actual type due to the rules of polymorphism
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

10Polymorphism - CSE 114 Computer Science I Polymorphism...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online