Week 9 Flashcards
A feature that allows us to perform an action in different ways
Polymorphism
Does polymorphism allow you to program in the general or the specific?
Polymorphism allows you to program in the general, rather than the specific
T/F - Polymorphism enables you to write programs that process objects that share the same superclass as if they were all objects of the superclass
True
A type defined by a subclass is called a _______, and a type defined by a superclass is called a _________
subtype, supertype
The key concept of polymorphism: “Relying on each object ‘knowing how…’ to do what?
Knowing how to do the right thing in response to the same method call
Why polymorphism powerful?
Makes the class flexible and extensible, simplifies programming by providing a single interface
Method overriding allows subclasses to do what?
Allow for the reuse of code
Subclasses can use all the general definitions the superclass provides and add specializations by overriding methods
What is the difference between polymorphism and inheritance?
Inheritance applies the behaviour of a superclass to a subclass.
Polymoprhism changes the behaviour of the superclass in the subclass.
These are useful for assigning common functionality to possibly unrelated classes:
Interfaces
2 unrelated classes implement the same interface, what happens with their methods and objects?
Classes that implement the same interface can have their objects respond to all of the interface method calls
T/F - There are more types of polymorphism than inheritance
False, there are 2 types of polymorphism, 5 types of inheritance
With polymorphism, we can design and implement systems that are _______ _________
easily extensible
T/F - Interaces are useful for assigning common functionality to possibly unrelated classes
True
T/F - Polymorphism enables you to deal in specifics
False, Polymorphism enables you to deal in generalities
T/F - Objects will behave in an appropriate manner even if you do not know their type, as long as they belong to the same inheritance hierarchy
True
How does polymorphism promote extensibility?
Software that invokes polymorphic behavior is independent of the object type that it is being used by
You can add new objects types without changing the base system
Only the client code that instantiates new objects must be changed
What is a polymorphic reference?
A variable that can refer to different types of objects at different points of time
T/F - All object references in Java are potentially polymorphic
True
T/F - A method invoked through a polymorphic reference cannot change form from one invocation to the next
False
Static polymorphism has method ________ing, dynamic polymorphism has method _________ing
Method overloading, method overriding
How does method overloading different from method overriding?
Overloading is done at compile time, overriding at run-time
Overidden vs. Overriding. How do they differ?
Overridden = parent class method
Overriding = child class method
Can the argument list differ when method overriding?
No, must be same type and sequence
I have a superclass method that is public, in the subclass I make the method private, will this work?
No. The access modifier cannot be more restrictive than the overridden method of the parent class
Can private, static, or final methods be overriden? What is the caveat?
No, no, and no. Static methods can be re-declared, which would make the method act differently be completely separated from the superclass method
Java defers method binding until run time, what is this called?
Dynamic binding, or late binding
What would happen if binding was done at compile time?
It would call the same method every time
Definition: Linking between the method call and method implementation is resolved at compile time
Static binding