Chapter 8 - Polymorphism Flashcards

1
Q

Definiton of polymorphism

A

Polymorphism in Java is like having a universal remote control. Imagine you have different types of electronic devices (TV, DVD player, sound system) with their own unique functions. The universal remote, in this case, is like a common interface or superclass.

Polymorphism is a concept by which we can perform a single task in different ways. That is, when a single entity (object) behaves differently in different cases, it is called polymorphism.

allows objects of different classes to be treated as objects of a common superclass. It enables you to write flexible and extensible code by providing a way to work with objects of multiple types without needing to know their specific implementations.

With polymorphism, you can use this universal remote to control any device, even though each device has its specific features. You don’t need to know the inner workings of each device; you can simply press the buttons on the universal remote, and it sends the right commands to the device you’re currently using.

In Java, this means you can write code that works with objects from various classes as long as they share a common superclass or interface. You can treat these objects uniformly, even though they may have different implementations behind the scenes. This flexibility simplifies your code and makes it more versatile.

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

Compile time polimrphism

A

Method Overloading:

Method overloading is a form of compile-time (static) polymorphism.
It allows you to define multiple methods in the same class with the same name but different parameters (different method signatures).
The Java compiler determines which method to call based on the number, type, and order of arguments provided during the method invocation.

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

Runtime polimrphism

A

Method Overriding:

Method overriding is a form of runtime (dynamic) polymorphism.
It occurs when a subclass provides a specific implementation of a method that is already defined in its superclass.
The overridden method in the subclass should have the same method signature (method name, return type, and parameter list) as the method in the superclass.
When you call an overridden method on an object of the subclass, the subclass’s implementation is executed.

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

Polymorphic Reference

A

Polymorphic reference is a concept in Java that allows you to create a reference variable of a parent class type and then assign it an object of a child class.

Animal myAnimal;
myAnimal = new Dog();

    myAnimal.makeSound();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Late Binding (Dynamic Binding)

A

In polymorphism, the method to be executed is determined at runtime (late binding), not at compile time.
This allows for flexibility in choosing the appropriate method implementation based on the actual object type.

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

How is Inheritance useful to achieve Polymorphism in Java?

A

Inheritance represents the parent-child relationship between two classes and polymorphism take the advantage of that relationship to add dynamic behavior in the code (or to make the program more dynamic).

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

What are the differences between Polymorphism and Inheritance in Java?

A

Inheritance helps in code reusability in child class by inheriting behavior from parent class. On the other hand, polymorphism enables child class to redefine already defined behavior inside parent class.

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

What is Binding in Java?

A

The connecting (linking) between a method call and method definition is called binding in java.

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

What are the types of binding in Java?

A

There are two types of binding in java. They are as follows:

a. Static Binding (also known as Early Binding).
b. Dynamic Binding (also known as Late Binding).

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

What is Static binding in Java?

A

The binding that happens during compilation is called static binding in java. This binding is resolved at the compiled time by the compiler.

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

How Java compiler performs static binding?

A

Java compiler just checks which method is going to be called through reference variable and method definition exists or not.

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

Why static binding is also called early binding in Java?

A

Static binding is also called early binding because it takes place before the program actually runs.

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

Give an example of static binding.

A

An example of static binding is method overloading.

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

What is Dynamic binding in Java?

A

The binding which occurs during runtime is called dynamic binding in java. This binding is resolved based on the type of object at runtime.

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

How JVM performs dynamic binding in Java?

A

In the dynamic binding, the actual object is used for binding at runtime. JVM resolved the method calls based on the type of object at runtime. The type of object cannot be determined by the compiler.

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

Why Dynamic binding is also called late binding in java?

A

Dynamic binding is also called late binding or runtime binding because binding occurs during runtime.

17
Q

Give an example of dynamic binding in Java.

A

An example of dynamic binding is method overriding.

18
Q

Why binding of private, static, and final methods are always static binding in Java?

A

Static binding is better performance-wise because java compiler knows that all such methods cannot be overridden and will always be accessed by object reference variable.

Hence, the compiler doesn’t have any difficulty in binding between a method call and method definition. That’s why binding for such methods is always static.