A case study of polymorphic usage in Java _java

Source: Internet
Author: User

The examples in this article describe the polymorphic usage in Java. Share to everyone for your reference. The specific analysis is as follows:

Polymorphism is the core feature of object-oriented programming language. Encapsulation, inheritance are relatively simple, so here only to polymorphic make a small note ...

1, what is polymorphism?

Polymorphism means that an object can have multiple characteristics and can display different states in a given situation, thus dealing with different properties and methods. In Java, a polymorphic implementation refers to the use of the same implementation interface to implement different object instances.

For example, we define a Parent class, and then define a GetName () method that returns a string that defines a member method dosomething (parent obj) with a formal parameter of the parent type, in which Obj.getname () is invoked. Then define two classes A and B, all inherited from the parent class, overriding the GetName () method in the subclass. Finally, you create an object OBJP of the parent class in the main method, call the Objp.dosomething () method, and pass the reference to class A, and Class B. Observe the output results.

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 ());
 Passing a reference to a, the GetName method of Class A is called
    objp.dosomething (New B ());
 Passing a reference to B, the GetName method of class 
a extends Parent 
{ 
  private String name = "Class A" 
  is invoked. @ Override GetName () method public 
  String getName () 
  {return 
    this.name; 
  } 
} 
Class B extends Parent 
{ 
  private String name = "Class B"; 
  @ Override GetName () method public 
  String getName () 
  {return 
    this.name; 
  } 
}

Thus, the DoSomething method of the parent class invokes the GetName method of the object we are passing, rather than the GetName method of the parent class itself.

2. Interface in Java

The interface in Java is the declaration of a series of methods. An interface has only the characteristics of the method, but not the implementation of the method. These methods can be implemented from elsewhere through a specific class. In Java, a keyword interface is used to declare an interface.

Using interfaces to implement Polymorphic instances:

Interface Name 
{ 
  //declaration only, do not implement public 
  String GetName (); 
} 
Class A implements name 
{ 
  private String name = ' Class A '; 
  Implement GetName method public 
  String getName () 
  {return 
    name; 
  } 
} 
Class B implements name 
{ 
  private String name = ' Class B ';
  Implement 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 Class A is printed out.

PS: A class must be declared abstract (abstract) if it does not implement all the methods in the interface. An abstract class is not allowed to instantiate an object.

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.