Book Image

Learning Selenium Testing Tools - Third Edition

Book Image

Learning Selenium Testing Tools - Third Edition

Overview of this book

Table of Contents (22 chapters)
Learning Selenium Testing Tools Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Polymorphism


Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Overloading

Methods with the same names in a class are allowed provided each method has a different signature. Overloading can be done in the following ways:

  • Number of arguments

  • Type of arguments

  • Position of arguments

An example of a Java program with overloading

The following is an example of overloading in a Java program:

package MyFirstPackage;

class SampleClass10 {
  void sampleMethod(){
  System.out.println("executing sample method");
}
  void sampleMethod(float b){
    System.out.println("executing sample method" + b);
  }
 }
 
  class SampleClass20 extends SampleClass10 {
    void sampleMethod1(){
    System.out.println("executing sample method 2");
  }
    void sampleMethod1(int a){
    System.out.println("executing sample method 2" + a);
    }
  }

public  class OverLoading {
  public static void main(String[] args) {
    SampleClass20 methodcall = new SampleClass20();
    methodcall.sampleMethod1();
    methodcall.sampleMethod();
    methodcall.sampleMethod1(12);
    methodcall.sampleMethod(1);
  }
}

The output will be as follows:

executing sample method 2
executing sample method
executing sample method 212
executing sample method1.0

Overriding

The subclass accessing the method of a superclass and changing its behavior as per the request of a subclass is called overriding.

An example of a Java program with overriding

The following is an example of overriding in a Java program:

package MyFirstPackage;

class SampleClass100 {
  void sampleMethod(){
  System.out.println("executing sample method");
}
  void sampleMethod(float b){
    System.out.println("executing sample method" + b);
  }
  }
  class SampleClass200 extends SampleClass100 {
    void sampleMethod(){
    System.out.println("overidding is done");
  }
    void sampleMethod1(int a){
      System.out.println("executing sample method 2" + a);
    }
  }

public  class OverRiding {
  
public static void main(String[] args) {
    SampleClass200 methodcall = new SampleClass200();
    methodcall.sampleMethod();
  }
}

The output will be as follows:

overriding is done