Thursday, August 8, 2024

Polymorphism

Polymorphism :

poly =  many

morphism  =  forms

Types of Polymorphism:

1. Static polymorphism (Compile-time Polymorphism):

2 .Dynamic Polymorphism (Run-time Polymorphism):

 

1. Static polymorphism (or) Compile time polymorphism:

  • ·  This type of polymorphism is resolved during compile time.
  • Method binding occurs at the time of compilation.
  • Example: Method Overloading.

 

Method binding refers to the process of associating a method call with the method definition. It determines which method implementation will be executed when a method is called.

 

2. Dynamic Polymorphism (or) RunTime polymorphism:

·  This type of polymorphism is resolved during runtime.

·  Method binding occurs at the time of execution.

·  Example: Method Overriding.

FAQ: Difference between Method Overloading and Method Overriding

1.  Def :  writing the same method by passing diff no of args

               add()

{

}

add(int a)

add(int a, int b);

passing diff data types

changing position of data types

 

MOR Def: 

        writng parent class methods in child class as it is with different logic or diff code

overriding - changing/modifying  the method body

class car

{

  void M1()

{

System.out.println(" to start car- insert keys and turn the keys to the right side");

}

}

class MaruthiCar extends car

{

  void M1()

{

System.out.println("Start button");

}

}

2. MOL: Can be done within the same class.

MOR: Requires at least two classes (parent and child).

 

3 . Method overloading is the example of compile time polymorphism.

Method overriding is the example of run time polymorphism.

 

4. In case of method overloading, parameter must be different.

   ex:   add(int a)

               add(int a, int b)

In case of method overriding,    parameter must be same.

ex:  

class Car {

    void start() {}

}

 

class MarutiCar extends Car {

    @Override

    void start() {}

}

 

5. Return type can be same or different in method overloading.  But you must have to change the parameter.

 void add()

{

}

int add(int a)

{

}

  

MOR : Return type must be same as parent class in method overriding.

 

Here’s the information in a tabular format:

Aspect

Method Overloading (MOL)

Method Overriding (MOR)

Definition

Writing multiple methods with the same name but different parameters (number, type, or order of arguments).

Writing a method in the child class with the same signature as a method in the parent class but with different logic or implementation.

Example

java<br>void add() {}<br>void add(int a) {}<br>void add(int a, int b) {}<br>

java<br>class Car {<br>void start() {<br>System.out.println("Insert keys and turn them to the right side.");}<br>}<br><br>class MarutiCar extends Car {<br>@Override<br>void start() {<br>System.out.println("Start button pressed.");}<br>}<br>

Class Requirement

Can be done within the same class.

Requires at least two classes (parent and child).

Polymorphism Type

Example of compile-time polymorphism.

Example of runtime polymorphism.

Parameters

Parameters must be different.
Example:
java<br>void add(int a) {}<br>void add(int a, int b) {}<br>

Parameters must be the same as in the parent class.
Example:
java<br>class Car {<br>void start() {}<br>}<br><br>class MarutiCar extends Car {<br>@Override<br>void start() {}<br>}<br>

Return Type

Can be the same or different, but you must change the parameters.
Example:
java<br>void add() {}<br>int add(int a) {}<br>

Must be the same as in the parent class.
Example:
java<br>class Car {<br>void start() {}<br>}<br><br>class MarutiCar extends Car {<br>@Override<br>void start() {}<br>}<br>

 

 

Accessing Instance Variables from an Interface:

// To access an instance variable from an interface, use: // Syntax: interfacename.variablename

 

ex:

package InterfaceBasics;

interface I1

{

               // instance variable

                              int a=10; //  default instan variable in interface  are constant-  final

                              // (or)  public static final int a=10;

                             

                              // FAQ Where did you use final variable in your project ?

                              // A final variable is used when we do not want to change the value throughout the project. // It is a constant.

                              // then we will go for final variable.

                              // Pi =3.134

                              // ex2: Project, we have diff folders - ConfigFilePath, AutomationResultsPath, TestDataPath, LogsFilePath

               final String configFilePath =  "C:\\brahma\\Practise\\SelniumPractiseNew\\March52024MyWorkspace\\SampleJavaProject2\\src\\Config.properties";

                             

                             

                             

                              // Only abstract methods

                              abstract void M1(); // incomplete method

}

interface I2

{

               int a=20;

}

public class testInterfaceVariable

{

               public static void main(String[] args) {

                              //  To accesss intance variable from interface -use

//                           ex:syntax :interfacename.varname

                             

                              System.out.println("Instance variable can be accessed using interface name I1 ="+  I1.a);

                              //Instance variable can be accessed using interface name  =10

                             

                              // access I2 interface variable a

                              System.out.println("Instance variable can be accessed usning interface name I2 ="+ I2.a);// 20

               }

}

 

 

Multiple Inheritance through Interface :

package MultipleInhertanceUsingInterface;

class A

{             

               int a=10;

}

class B

{

               int a=20;

}

public class testMultipleInheritance extends A,B //  assume

{

               // get parent class   A ,B variable into child class

                                                                                                                        //            int a=10; from Class -A

                                                                                                                        // int a=20;  from class - B

               public static void main(String[] args) {

                             

                              testMultipleInheritance t1 = new testMultipleInheritance();

                              t1.a // should it display a =10  or a =20 there ia abiguity b/w 2 variables

So Multiple Inheritance is not possible using class concept

               }

}

Multiple Inheritance using Interface:

package InterfaceBasics;

interface Father

{

               // define variable  height = 6.0f;

               float height = 6.0f;

              

               // Define calculateHeight()

                              //  abstract Method

               abstract void calculateHeight();  

}

interface Mother

{

               // define variable height = 4.0f;

               float height = 4.0f;

               // Define calculateHeight()

               abstract void calculateHeight();

}

public class testChildMultipleInertiance2 implements Father,Mother

{

               //implements calculateHeight - Father, Mother

              

                                             //  child ht =  ( father ht +   mother ht )/2

               public void calculateHeight()

               {

                 float childHeight = (Father.height + Mother.height)/2;

                 //                     (6.0 + 4.0) /2

                 //                      10.0 /2

                 //                     5.0

                 //childHeight  = 5.0

                 System.out.println("childHeight ="+childHeight);

               }                            

                              public static void main(String[] args) {

                                                            // create obj for class-testChildMultipleInertiance2

                                             testChildMultipleInertiance2  tref = new testChildMultipleInertiance2();

                                            

                                             //  call calculateHeight();

                                             tref.calculateHeight();

                                                           

                              }

               }

o/p:

childHeight =5.0

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...