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
how to do downcasting
Sale otherSale = Sale(otherObject)
how to check if downcasting is legitimate
downcasting to a specific type is only sensible if the object being cast is an instance of that type – use the instance of
what does instanceof return
returns true if object is of class type or if object is an instance of any descendent class
where does every object inherit a clone method from
class Object
what does clone do
returns a deep copy of the calling object -each class must override it
define covariant return types
google it
invoking the clone method
Sale copy = orginalObj.clone()
Sale copy = (Sale)original.clone();
when must you use the clone method
when dealing with inheritance
what are the limitations of copy constructors
polymorphism doesnt work with methods of different names - you will loose the fact that the Sale is a DiscountSale
what is an abstract class
“postpone the definition of a method until we know the exact type that that method needs to work on”
In order to postpone the definition of a method, Java allows an abstract method to be declared An abstract method has a heading, but no method body The body of the method is defined in the derived classes The class that contains an abstract method is called an abstract class
What is a concrete class
a class that has no abstract methods
can you create instances of abstract classes?
no - abstract classes can only be used to derive more specialized classes abstract class constructor cannot be used to create an object of the abstract class - however in a derived class the constructor will include an invocation of the abstract class constructor in the form of super
can you have an abstract class as a parameter
Although an object of an abstract class cannot be created, it is perfectly fine to have a parameter of an abstract class type -This makes it possible to plug in an object of any of its descendent classes
It is also fine to use a variable of an abstract class type, as long is it names objects of its concrete descendent classes only