This article describes the use of polymorphism in Java with examples. Share it with everyone for your reference. The specific analysis is as follows:
Polymorphism is the core feature of object-oriented programming languages. Encapsulation and inheritance are relatively simple, so I will only make a small note on polymorphism here. . .
1. What is polymorphism?
Polymorphism means that an object can have multiple characteristics and can exhibit different states under specific circumstances to respond to different properties and methods. In Java, polymorphic implementation refers to using the same implementation interface to implement different object instances.
For example, we define a Parent class, then define a getName() method to return a string, define a member method doSomething(Parent obj) whose formal parameter is the Parent type, and call obj.getName() in this method. Then define two classes A and B, both of which inherit from the Parent class, and override the getName() method in the subclass. Finally, create an object objP of the Parent class in the main method, call the objP.doSomething() method and pass it the references of class A and class B. Observe the output.
class Parent { private String name = "parent"; public String getName() { return this.name; } public void doSomething(Parent obj) { //Output class name System.out.println(obj.getName()); } public static void main(String[] args) { Parent objP = new Parent(); objP.doSomething(new A()); // When passing a reference to A, the getName method of class A is called objP.doSomething(new B()); // When a reference to B is passed, the getName method of class B is called} } class A extends Parent { private String name = "class A"; //@Rewrite the getName() method public String getName() { return this.name; } } class B extends Parent { private String name = "class B"; //@Rewrite getName() Method public String getName() { return this.name; } }
It can be seen that the doSomething method of the parent class polymorphically calls the getName method of the object we passed, rather than the getName method of the Parent class itself.
2. Interface in Java
An interface in Java is a declaration of a series of methods. An interface only has the characteristics of methods, but not the implementation of methods. These methods can be implemented from elsewhere through specific classes. In Java, the keyword interface is used to declare an interface.
Examples of polymorphism using interfaces:
interface Name { //Only declare, not implement public String getName(); } class A implements Name { private String name = "class A"; //Implement the getName method public String getName() { return name; } } class B implements Name { private String name = "class B"; //Implement the getName method public String getName() { return name; } public static void main(String[] args) { Name obj = new A(); System.out.println(obj.getName()); } }
As you can see, the name of class A, class A, is printed.
PS: If a class does not implement all methods in the interface, then the class must be declared abstract. An abstract class does not allow instantiation of objects.
I hope this article will be helpful to everyone’s Java programming.