Polymorphism, Question 8

What is polymorphism

Polymorphism means that the same method of an instance of a class takes on different forms in different situations. The polymorphism mechanism enables objects with different internal structures to share the same external interface. This means that although the specific operations for different objects are different, they are represented by a common class (those operations)It can be called in the same way. Polymorphism is the same interface that uses different instances to perform different operations, as shown in the diagram:

Polymorphisms are manifestations of multiple representations of an object. In reality, for example, when we press the F1 key:

  • If the current pop-up under the Flash interface is the help document for AS 3;

  • If the current pop-up under Word is Word Help;

  • Windows Help and Support pops up under Windows.

The same event occurs on different objects with different results.

Realization Conditions for Polymorphism

Three conditions for polymorphism:

  • The existence of inheritance (inheritance is the basis of polymorphism, there is no polymorphism without inheritance);

  • Subclass overrides the method of the parent class (calling the method of subclass override under polymorphism);

  • A parent reference variable points to a subclass object (a type conversion from subclass to parent).

Rules for converting a subclass to a parent class:

  • References to a parent class to objects of a subclass, called upcasting, are automatically type-converted. The method invoked by a parent reference at this time is a method that the subclass overrides or inherits from the parent class, not a method of the parent class. It is not possible to invoke a method specific to the subclass through a parent reference variable at this time;

  • If a parent class invokes a method specific to a subclass, it must assign a reference to a subclass that points to a subclass object, called a downward transition, where a cast must be performed.

The following is a demonstration of a polymorphic instance, detailed in the comments:

public class TestAnimalDemo {
public static void main(String[] args) {
show(new Cat()); // Call show method with Cat object
show(new Dog()); // Call show method with Dog object

Animal a = new Cat(); // Upward Transition
a.eat(); // Cat's eat was called
Cat c = (Cat) a; // Downward transition
c.work(); // Called Cat work
}

public static void show(Animal a) {
a.eat();
// Type Judgment
if (a instanceof Cat) { // What cats do
Cat c = (Cat) a;
c.work();
} else if (a instanceof Dog) { // What Dogs Do
Dog c = (Dog) a;
c.work();
}
}
}

abstract class Animal {
abstract void eat();
}

class Cat extends Animal {
public void eat() {
System.out.println("Eat fish");
}

public void work() {
System.out.println("Catching mice");
}
}

class Dog extends Animal {
public void eat() {
System.out.println("Eat Bones");
}

public void work() {
System.out.println("Housekeeping");
}
}

Output results: fish-catching mice eat bones, watch the house eat fish-catching mice

You can use instanceof to determine whether a class implements an interface or whether an instance object belongs to a class. The syntax format of instanceof is:

 
  1. Object instanceof class (or interface)

Its return value is Boolean, or true, or false. ##### The implementation of polymorphism can take two forms in Java: inheritance and interface.

  • Polymorphism based on inheritance implementation

The implementation mechanism based on inheritance is mainly manifested in the override of some methods by the parent class and one or more subclasses inheriting the parent class, and the override of the same method by multiple subclasses can exhibit different behaviors.

Polymorphisms based on inheritance implementations can be summarized as follows: For a parent type that references a subclass, it applies to all subclasses that inherit that parent class when dealing with the reference. Different subclass objects will result in different implementations of methods and different behaviors when performing the same actions.

If the parent class is an abstract class, then the subclass must implement all the abstract methods in the parent class, so that all the subclasses of the parent class must have a uniform external interface, but their internal implementations can vary. In this way, we can use the uniform interface provided by the top class to handle the method at that level.

  • Interface-based Polymorphism

Inheritance is represented by overriding several different subclasses of the same method of the parent class, which may be represented by implementing the interface and overriding several different classes of the same method in the interface.

In the polymorphism of an interface, a reference to an interface must be an instance program that specifies a class that implements the interface and, at run time, executes the corresponding method based on the actual type of object reference.

Inheritance is single inheritance and can only provide a consistent service interface for a set of related classes. However, an interface can be multiple inheritance and multiple implementation, it can be combined and expanded with a set of related or unrelated interfaces, and it can provide a consistent service interface to the outside world. So it has more flexibility than inheritance.

Programming Requirements

As prompted, supplement the code in the right editor:

  • Declare an Animal class in which the eat() method is defined;

  • Declare that the Dog, Cat, and Lion classes all inherit from the Animal class and override the eat() method;

  • Instantiate the subclass object in a polymorphic manner and call the eat() method to print the output information;

  • See the test instructions for specific output requirements.

Test Instructions

Test input: Unexpected output: eating bread...Eating rat...Eating meat...

package case8;
public class TestPolymorphism {
	public static void main(String[] args) {
		// Instantiate subclass objects individually in a polymorphic manner and call the eat() method
		/********* begin *********/
		 Animal a = new Dog(); 
        a.eat(); 
		 Animal b = new Cat(); 
        b.eat(); 
		 Animal c = new Lion(); 
        c.eat(); 
		/********* end *********/
	}
}
// Define eat() method in Animal class
abstract class Animal {
	/********* begin *********/
abstract void eat();
	/********* end *********/
}

// Dog class inherits Animal class override eat() method
class Dog extends Animal {
	/********* begin *********/
public void eat() {
        System.out.println("eating bread...");
    }
	/********* end *********/
}

// Cat class inherits Animal class override eat() method
class Cat extends Animal {
	/********* begin *********/
public void eat() {
        System.out.println("eating rat...");
    }
	/********* end *********/
}

// The Lion class inherits the Animal class override eat() method
class Lion extends Animal {
	/********* begin *********/
public void eat() {
        System.out.println("eating meat...");
    }
	/********* end *********/
}

 

Keywords: Java

Added by wempy on Fri, 15 Oct 2021 20:08:20 +0300