polymorphism Flashcards
define polymorphism
ability to associate many meanings to one method name - late binding is used to do so
difference between inheritance and polymorphism
inheritance allows a base class to be defined and in other classes derived from it - code from base classes can then be used for its own and derived objects
polymorphism allows changes to be made to the method definitions in the derived classes and have those changes appled to the software written for the base class
what is BINDING
process of associating a method definition with a method invocation
what is early binding
if the method definition is associated with its invocation when the code is actually compiled
what is late binding
if the method definition is associated with its invocation when the method is invoked at run time
java uses late binding for all methods except…
private
static
final
because of late binding a method can be written in a base class to perform a task even if
portions of that task aren’t defined yet
static binding
when the decision of which definition of a method to use is made at compile time
this decision is made based on the type of the variable naming object
when does java use static binding
private
static
final
late binding for private and final would make no difference - it does make a difference in static methods
what does the final keyword mean
no sub-classes can be made
this method cannot be overridden
expand the statement: an object knows the definitions of its methods
the type of class variable determines which method names can be used with the variable
however, the object names by the variable determines which definition with the same method name is used
what is the special case of the rules mentioned above?
the type of a class parameter determines which method names can be used with the parameter the argument determines which definition of the method name is used
define upcasting
when an object of a derived class is assigned to a variable of a case class or any ancestor class
- late binding occurs
e. g. discount sale into a sale
what is downcasting
when a type cast is performed from a base class to a derived class (or from any ancestor or descendent class)
pitfalls with downcasting
must be done very carefully
cant put a specialized object into a general object
can only be done when you know for sure that the general object can actually fit into the derived class
e.g. sale into a discount sale