Polymorphic, multi-inheritance

Source: Internet
Author: User

Polymorphic:

The manifestation of inheritance is polymorphism. A parent class can have more than one subclass, whereas in subclasses you can override the parent class's methods (for example, method print ()) so that the code that is overridden in each subclass is different and the natural representation is different. This uses the parent class's variable to refer to the different subclasses, the result and the representation of the same method print () is different, this is polymorphic, the same message (that is, call the same method) will have the same result.

Multiple inheritance:

. Java does not have many inheritance, and the interface implementation of multiple inheritance! A class or you can implement multiple interfaces at the same time! (It is equivalent to a class in C + + that inherits multiple classes at the same time!) )

However: A interface can extends multiple other interface

You can define an interface to inherit multiple interfaces, and then implement this interface when used, equivalent to implementing methods in all interfaces.

  interface facilitates the expansion of code functions (adding new functionality)! Inheritance, in turn, facilitates the modification of the code function (changing the old function into a new function)! here the old features of the modification and the addition of new features of the premise is not to modify the old features, old features can still be used! The old class code is not modified!

1. Modify the old functionality with inheritance.

Now if a generation of Son1 function hit the child, that is, Method Hitchild () inside the function of the implementation of the problem, we want to change the implementation of the concrete, then we can rewrite a new second generation subclass Son11 inherit from the generation of sub-class Son1, and rewrite its hitchild () method.

<ccid_nobr>
<ccid_code>//second generation subclass public class Son11 extends son1{//overriding parent class hitting kids method public void Hitchild () {System.out.println (" I am a new implementation of the second generation subclass for this method "); }}

2. Add new functionality with interface implementations.

Now that the class Son1 has a ' spanking ' function (that is, the method), and we have a new requirement, we need it to have a new function of ' spoil the child ' (new Method Pet ();). And the old code can't move! Then we can define a new interface (Petinterface) that defines the declaration of this method in the interface pet (). Re-write a second-generation subclass Son11, let it inherit class Son1 first, and implement Interface petinterface! and implement the Pet () method in the interface!

<ccid_nobr>
<ccid_code>//new Interface public interface petinterface{//new function method public void pet (); Second generation subclass public class Son11 extends Son1 implements petinterface{//Implement interface method public void Pet () {System.out.println ("Father loves me very much!") "); }}

So this second generation subclass has the old method, but also has the new function

(one of the biggest differences between Java interfaces and Java abstract classes is that Java abstract classes can provide partial implementations of some methods, while Java interfaces cannot)

Polymorphic, multi-inheritance

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.