Polymorphism Flashcards

1
Q

What is Polymorphism?

A

Poly=many morph=forms

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

Why Polymorphism?

A

Polymorphism lets programmers sacrifice specificity for generality and treat any number of classes as their lowest common denominator and limited to methods declared in that denominator.

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

What is Dynamic Binding?

A

Dynamic binding, which is when actual method implementation used is not determined until runtime and in contrast with static binding, in which method gets resolved at compile time

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

Why Declared Type?

A

declared type keeps things generic and can reference a lot of objects using one generic type

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

Why Actual Type?

A

Actual type ensures specificity and when defining implementing class, the method can get implemented in any way

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

Inheritance models, which relationship?

A

Inheritance models “is a” relationship

Interface models “acts as” relationship

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

Is multiple inheritances allowed in java?

A

No, in java you can only inherit from one superclass due to the diamond problem.

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

Types of polymorphism

A

In Java polymorphism is mainly divided into two types:

Compile-time Polymorphism
Runtime Polymorphism

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

Compile Time Polymorphism

A

It is also known as static polymorphism. This type of polymorphism is achieved by function overloading.

Functions can be overloaded by a change in the number of arguments or/and a change in the type of arguments.

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

Runtime Polymorphism

A

It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime.
This type of polymorphism is achieved by Method Overriding.

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

Runtime Polymorphism (data members)

A
In Java, we can override methods only, not the variables(data members), so runtime polymorphism cannot be achieved by data members. For example :
class A
{
    int x = 10;
}
// class B
class B extends A
{
    int x = 20;
}
// Driver class
public class Test
{
    public static void main(String args[])
    {
        A a = new B(); // object of type B
        // Data member of class A will be accessed
        System.out.println(a.x); //10
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Static vs Runtime Binding

A

Static binding is done during compile-time while the dynamic binding is done during run-time.
private, final, and static methods and variables use static binding and bonded by the compiler while overridden methods are bonded during runtime based upon the type of runtime object

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