Home > Java > javaTutorial > Example analysis of polymorphic usage in Java

Example analysis of polymorphic usage in Java

高洛峰
Release: 2017-01-19 13:57:01
Original
1377 people have browsed it

The examples in this article describe the usage of polymorphism in Java. 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 show 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) with a formal parameter of 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) 
  { 
    //输出类名 
    System.out.println(obj.getName()); 
  } 
  public static void main(String[] args) 
  { 
    Parent objP = new Parent(); 
    objP.doSomething(new A());
 // 传递A的引用,则调用的是A类的getName方法
    objP.doSomething(new B());
 // 传递B的引用,则调用的是B类的getName方法
  } 
} 
class A extends Parent 
{ 
  private String name = "class A"; 
  //@重写getName()方法 
  public String getName() 
  { 
    return this.name; 
  } 
} 
class B extends Parent 
{ 
  private String name = "class B"; 
  //@重写getName()方法 
  public String getName() 
  { 
    return this.name; 
  } 
}
Copy after login

It can be seen that the doSomething method of the parent class polymorphically calls the getName method of the object we passed, instead of the getName method of the Parent class itself.

2. Interface in Java

The 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.

Example of using interface to implement polymorphism:

interface Name
{
  //只声明,不实现
  public String getName();
}
class A implements Name
{
  private String name = "class A";
  //实现getName方法
  public String getName()
  {
    return name;
  }
}
class B implements Name
{
  private String name = "class B";
  //实现getName方法
  public String getName()
  {
    return name;
  }
  public static void main(String[] args)
  {
    Name obj = new A();
    System.out.println(obj.getName());
  }
}
Copy after login

As you can see, the name of class A, class A, is printed.

PS: If a class does not implement all the 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.

For more articles related to analysis of examples of polymorphic usage in Java, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template