Polymorphism in Java

Source: Internet
Author: User

Polymorphism is an important feature of object-oriented, it is simple to say: "An interface, a variety of implementations," is the same thing shows a variety of forms.

Here's a quick overview of the code:

Three conditions must be met to achieve polymorphism

1 must have inheritance

2 must be rewritten

3 Child class object must be referenced by parent class

public class Wine {
public void Fun1 () {
System.out.println ("The fun of Wine ...");
Fun2 ();
}

public void fun2 () {
System.out.println ("Wine Fun2 ...");
}
}

public class JNC extends wine{
/**
* @desc Subclasses overloads the parent class method
* The method does not exist in the parent class, and the parent class cannot reference the method after it has been transformed.
* @param a
* @return void
*/
public void Fun1 (String a) {
System.out.println ("JNC Fun1 ...");
Fun2 ();
}

/**
* Subclasses overriding parent class methods
* When referring to the parent class of a subclass called Fun2, the method must be called
*/
public void fun2 () {
System.out.println ("JNC Fun2 ...");
}
public static void Main (string[] args) {
Wine a = new JNC ();
A.fun1 ();
}
}

Polymorphism in Java

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.