Answers for "Java Polymorphism"

8

polymorphism in java

POLYMORPHISM: It is an ability of object to behave in multiple
form. The most common use of polymorphism is Java, when a
parent class reference type of variable is used to refer to a child
class object.
In my framework
  E.g.: WebDriver driver = new ChromeDriver();
We use method overloading and overriding to achieve
Polymorphism.

There are two types of achieving Polymorphism
Upcasting & Downcasting
Upcasting is casting a subtype to a supertype, 
going up in the inheritance tree.
It is done implicitly
 in order to use method available on any
 interface/class, the object should be of
 same class or of class implementing the interface.  
   WebDriver driver = new ChromeDriver();
or
	TakeScreenShot ts = new ChromeDriver();
    
    Downcasting is casting to a subtype, 
going down in the inheritance tree.
It is done to access sub class features.
It has to be done manually

ChromeDriver driver = (ChromeDriver)Webdriver;
Posted by: Guest on November-28-2020
0

Java Polymorphism

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {					//extends Animal
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {					//extends Animal
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}
Posted by: Guest on October-15-2021

Code answers related to "Java Polymorphism"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language