Java Multiple inheritance

Source: Internet
Author: User
Tags multiple inheritance in java

As we all know, there are only single-inheritance programming languages in the Java object-oriented language, and you might say that it is possible to achieve multi-inheritance by implementing multiple interfaces in a flexible way. Yes, you are right, but this is not the article to mention the content of this article is to talk about the real existence of Java in the multi-inheritance structure, nonsense said, let's go into the subject.
In fact, speaking of inheritance, everyone's first impression is certainly the inheritance of the class, as far as Java is concerned, the inheritance of the class is indeed a single inheritance, in this regard, undoubtedly, Java syntax and compiler will this point of the dead, suppose you try to let a class through the extends keyword inherits more than one parent class , compilation must pass.

The multi-inheritance that is said here is the multiple inheritance of the interface, and we know that when the class implements the interface, it uses the Implements keyword to implement multiple interfaces. An important feature of extensibility Java, if we want to extend the existing multiple interfaces, because it is not the implementation of the interface, we can not use the Implements keyword, then what to do, then you can only use the extends keyword. Here's a look at the code:

package com.lib.ThinkInJava.mutilExtends; public interface lethal {void Kill ();} package com.lib.ThinkInJava.mutilExtends; public interface monster {void Destroy ();} package com.lib.ThinkInJava.mutilExtends; public interface vampire extends Monster, lethal {void Drinkblood ();}        

Vampire interface inherits the Monster,lethal two interfaces, and uses the keyword extends, in the compiler also does not have the error, is not very interesting, this is the Java multi-inheritance.

Note: Multiple inheritance in Java applies only to interfaces, classes do not inherit more, only single inheritance

Java Multiple inheritance

Related Article

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.