polymorphism Flashcards

1
Q

define polymorphism

A

ability to associate many meanings to one method name - late binding is used to do so

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

difference between inheritance and polymorphism

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is BINDING

A

process of associating a method definition with a method invocation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is early binding

A

if the method definition is associated with its invocation when the code is actually compiled

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is late binding

A

if the method definition is associated with its invocation when the method is invoked at run time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

java uses late binding for all methods except…

A

private
static
final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

because of late binding a method can be written in a base class to perform a task even if

A

portions of that task aren’t defined yet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

static binding

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

when does java use static binding

A

private
static
final

late binding for private and final would make no difference - it does make a difference in static methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does the final keyword mean

A

no sub-classes can be made

this method cannot be overridden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

expand the statement: an object knows the definitions of its methods

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is the special case of the rules mentioned above?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

define upcasting

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is downcasting

A

when a type cast is performed from a base class to a derived class (or from any ancestor or descendent class)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

pitfalls with downcasting

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to do downcasting

A

Sale otherSale = Sale(otherObject)

17
Q

how to check if downcasting is legitimate

A

downcasting to a specific type is only sensible if the object being cast is an instance of that type – use the instance of

18
Q

what does instanceof return

A

returns true if object is of class type or if object is an instance of any descendent class

19
Q

where does every object inherit a clone method from

A

class Object

20
Q

what does clone do

A

returns a deep copy of the calling object -each class must override it

21
Q

define covariant return types

A

google it

22
Q

invoking the clone method

A

Sale copy = orginalObj.clone()

Sale copy = (Sale)original.clone();

23
Q

when must you use the clone method

A

when dealing with inheritance

24
Q

what are the limitations of copy constructors

A

polymorphism doesnt work with methods of different names - you will loose the fact that the Sale is a DiscountSale

25
Q

what is an abstract class

A

“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
26
Q

What is a concrete class

A

a class that has no abstract methods

27
Q

can you create instances of abstract classes?

A
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
28
Q

can you have an abstract class as a parameter

A
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