Get the App
SLTechnology News&Howtos  ›  Development  › 

Default method and multiple inheritance instance Analysis in Java8

Shulou Source: shulou.com Published: 2022-06-01 11:49:10 09月10日 Update

This article "default method and multiple inheritance instance analysis in Java 8" article knowledge point most people do not understand, so Xiaobian summarized the following content for you, detailed content, clear steps, with a certain reference value, I hope you can read this article to gain something, let's take a look at this article "default method and multiple inheritance instance analysis in Java 8" article bar.

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit properties and attributes from multiple parent objects or classes.

Default methods in Java 8 can be seen as a form of multiple inheritance (except that properties cannot be inherited). Consider the following example, where the Button class implements two interfaces- Clickable and Accessible.

Each interface defines a default method. Therefore, the Button class can invoke methods from two interfaces. It's like multiple inheritance.

interface Clickable{ default void click(){ System.out.println("click"); }} interface Accessible{ default void access(){ System.out.println("access"); }} public class Button implements Clickable, Accessible { public static void main(String[] args) { Button button = new Button(); button.click(); button.access(); }}

If the interfaces of both implementations define a default method with the same method signature, the implementation class does not know which default method to use. An implementation class should explicitly specify the default method to use or define its own method. In the following example, both Clickable and Accessible define the print() method. In the Button class, the print() method specifies the default method.

interface Clickable{ default void click(){ System.out.println("click"); } default void print(){ System.out.println("Clickable"); }} interface Accessible{ default void access(){ System.out.println("access"); } default void print(){ System.out.println("Accessible"); }} public class Button implements Clickable, Accessible { public void print(){ Clickable.super.print(); Accessible.super.print(); } public static void main(String[] args) { Button button = new Button(); button.click(); button.access(); button.print(); } }

The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation class. This way, the interface remains compatible with older versions. This is a cool feature. However, we should keep in mind the motivation for using default methods and should keep interfaces and implementations separate.

The above is about the content of this article on "Default method and multiple inheritance instance analysis in Java 8". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

Tags: Methods interfaces content examples case analysis two objects characteristics motivations articles knowledge examples articles facets same value functions multiple most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Shulou Information Redmi Docker