Automation QA Testing Course Content

Polymorphysm Information


Polymorphysm:When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc.

In java, we use method overloading and method overriding to achieve polymorphism.

Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism.
We can perform polymorphism in java by method overloading and method overriding.

If you overload the static method in java, it is an example of compile-time polymorphism. Here,
 we will focus on runtime polymorphism in java.

Method Overloading:
If a class has multiple methods by the same name but different parameters, it is known as
Method Overloading.

If we have to perform only one operation, having the same name of the methods increases the
 readability of the program.

overloading: The payment option on any eCommerce website has several options like net banking, COD, credit card, etc. That means a payment method is overloaded several times to perform a single payment function in various ways.

Advantage of method overloading?

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java
By changing the number of arguments
By changing the data type of the parameter
Note: In java, Method Overloading is not possible by changing the return type of the method.
Que) Why Method Overloading is not possible by changing the return type of the method?

In java, method overloading is not possible by changing the return type of the method
because there may occur ambiguity.

Can we overload the main() method?

Yes, by method overloading. You can have any number of main methods in a class by method overloading. Let's see a simple example:

class Overloading1{ 
  public static void main(int a){ 
  System.out.println(a); 
  } 
   
public static void main(String s){
System.out.println(s)

}
  public static void main(String args[]){ 
  System.out.println("main() method invoked"); 
  main(10);
main("java"); 
  } 
} 
QUIZ on Method Overloading:
1)
package polymorph;

public class Test {

public static void main(String[] args) {
print();

}
public static void print(double...a) {
System.out.println("Double");
}

public static void print(int...a) {
System.out.println("Int");
}
}
a)Double
b)Int
c)Exception
-----------------------------------------------------------------------------------------

===========================================================
Method Overriding in Java

If the subclass (child class) has the same method as declared in the parent class, it is known as
 method overriding in java.

In other words, If the subclass provides the specific implementation of the method that has
 been provided by one of its parent classes, it is known as method overriding.

Usage of Java Method Overriding

Method overriding is used to provide specific implementation of a method that is already
provided by its super class.
Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

method must have same name as in the parent class
method must have same parameter as in the parent class.
child class implementation should be different than parent class method.
use inhertitance between classes

Runtime Polymorphism in Java

Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an
overridden method is resolved at runtime rather than compile-time.

In this process, an overridden method is called through the reference variable of a superclass.

 The determination of the method to be called is based on the object being referred to
 by the reference variable.

Let's first understand the upcasting before Runtime Polymorphism.

Upcasting

When reference variable of Parent class refers to the object of Child class,
it is known as upcasting. For example:
Parentclass  refvariable=new childclass(); //upcasting
Upcasting in java
must be IS-A relationship (inheritance)

Can we override static method?

No, static method cannot be overridden. It can be proved by runtime polymorphism,
so we will learn it later.


Why we cannot override static method?

because static method is bound with class whereas instance method is bound with object.
Static belongs to class area and instance belongs to heap area.


Can we override java main method?

No, because main is a static method.
-------------------------------------------------------------------------------------------------------
can you override the private method?
Ans)No, because private methods are not accessible in another class.
------------------------------------------------------------------------------------------------------
how can you stop overriding the methods?
Ans)by making private,static and final methods
--------------------------------------------------------------------------------------------------------

overriding: suppose there is a method getInterestRate() which returns the interest rate of a bank. RBI is the superclass and it returns 7 for getInterestRate(). There are various banks like sbi, axis, icici, etc which extend RBI class and override the getInterestRate() method to return 7.5, 8, 8.5, etc respectively.

-------------------------------------------------------------------------------------------------------
Difference between method overloading and method overriding in java
There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below:
No.
Method Overloading
Method Overriding
1)
Method overloading is used to increase the readability of the program.
Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2)
Method overloading is performed within class.
Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3)
In case of method overloading, parameter must be different.
In case of method overriding, parameter must be same.
4)
Method overloading is the example of compile time polymorphism.
Method overriding is the example of run time polymorphism.
5)
In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.
Return type must be same or covariant in method overriding.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.