csc2700lect03 - CSc 2700: Lecture # 3 Polymorphism...

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

View Full Document Right Arrow Icon
CSc 2700: Lecture # 3 Polymorphism September 4, 2008 1 Introduction In your CS/1 programming course you may have learned that polymorphism is one of the fundamental characteristics of any object-oriented program. Defnition 1. Selecting a method among several methods that have the same name on the basis of the actual types of the implicit parameter. For example, we may have a class that has multiple constructors - a default constructor and several parameterized constructors. One instantiates the class, the version of the constructor used, depends on which arguments are supplied to the constructor. In practical terms, polymorphism means that if class B inherits from class A, it does not have to inherit everything about class A; it can do some of the things that class A does di±erently. This means that every object of class B is also an object of class A since class B extends the behavior of class A. So in Java, the type of a variable does not completely determine the type of object to which it refers. So a variable of class A (the superclass) can hold an object of class B (the subclass). The way the variable works depends on the context in which it is used. The subclass automatically invokes the superclass’s method if it does not override it. 2 Polymorphism and InterFace Types An interface variable can hold reference to an object of a class that imple- ments the interface. Suppose we have an interface Measurable, then both of these assignment statements are valid: Measurable x; x = new BankAccount(10000); x = new Coin(0.1, "dime"); 1
Background image of page 1

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

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

Page1 / 6

csc2700lect03 - CSc 2700: Lecture # 3 Polymorphism...

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

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