Polymorphism

A

Ajay Raja

Guest


Virtual functions means?[To be discussed]

What is polymorphism?

  • Polymorphism means "many forms".
  • it occurs when we have many classes that are related to each other by inheritance.
  • Polymorphism allows us to perform a single action in different ways.

Why we need Polymorphism?

  • Polymorphism is a core concept in oops, is used to create more flexible, reusable, and maintainable code.

Key features of polymorphism:
1.Method Overriding
2.Method Overloading

1.Method Overriding:

  • This should be called as Run time polymorphism.
  • A child class can redefine a method of its parent class.
  • Overriding in Java occurs when a subclass or child class implements a method that is already defined in the superclass or base class.
  • The subclass method must match the parent class method's name, parameters, and return type.



Why we use @override[To be disscused]

2.Method Overloading:

  • This should be called as compile-time polymorphism.
  • We can define multiple methods with the same name but different parameters.
  • The parameter lists of overloaded methods must differ.

Code:
Number of parameters: 
For example,
add(int a, int b) and add(int a, int b, int c). 
Data types of parameters: 
For example, 
display(int x) and display(String s).
Order of parameters: 
For example, print(int a, double b) and print(double b, int a)



1)Compile time polymorphism:


  • Compile-Time Polymorphism in Java is also known as static polymorphism and also known as method overloading.


  • This happens when multiple methods in the same class have the same name but different parameters.

2)Run time polymorphism:

  • Runtime Polymorphism in Java known as Dynamic Method Dispatch.

Dynamic Methods Dispatch means??[TBD]

Early Binding:[static]

  • The binding which can be resolved at compile time by the compiler is known as static or early binding.

Late binding:[dynamic]

  • compiler doesn't decide the method to be called
  • Overriding is a perfect example of dynamic binding.
  • In overriding both parent and child classes have the same method.



Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top