Default Methods in Java 8 and Multiple Inheritance
2015-01-03 11:06
507 查看
From Wiki,
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.
Default methods in Java 8 can be viewed as a form of multiple inheritance (except that attribute can not be inherited). Consider the example below, the Button class implements two interfaces - Clickable and Accessible.
![](http://www.programcreek.com/wp-content/uploads/2014/12/default-methods-java-8.png)
Each interface defines a default method. The Button class therefore can call methods from both interfaces. This is like a multiple inheritance.
If both of the implemented interfaces define a default method with same method signature, then the implementation class does not know which default method to use. The implementation class should define explicitly specify which default method to use or define
it's own one. In the example below, Clickable and Accessible both define the print() method. In the Button class, the print() method specifies the default method.
![](http://www.programcreek.com/wp-content/uploads/2014/12/default-methods-java-8-same-name.png)
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 classes. In this way, the interface is still compatible with older versions.
This is a cool feature. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.
To know more features of Java 8, check out Simple Java 8.
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.
Default methods in Java 8 can be viewed as a form of multiple inheritance (except that attribute can not be inherited). Consider the example below, the Button class implements two interfaces - Clickable and Accessible.
![](http://www.programcreek.com/wp-content/uploads/2014/12/default-methods-java-8.png)
Each interface defines a default method. The Button class therefore can call methods from both interfaces. This is like a 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 both of the implemented interfaces define a default method with same method signature, then the implementation class does not know which default method to use. The implementation class should define explicitly specify which default method to use or define
it's own one. In the example below, Clickable and Accessible both define the print() method. In the Button class, the print() method specifies the default method.
![](http://www.programcreek.com/wp-content/uploads/2014/12/default-methods-java-8-same-name.png)
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 classes. In this way, the interface is still compatible with older versions.
This is a cool feature. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.
To know more features of Java 8, check out Simple Java 8.
相关文章推荐
- Multiple Inheritance in Java and Composition vs Inheritance
- The Default Methods And Static Methods In Java Interface
- Working with hashCode and equals methods in java
- Java Methods for Financial Engineering: Applications in Finance and Investment
- Working with hashCode and equals methods in java
- Interface in Java 8(Default/Static methods)
- Hidden Gems in the Java APIs - Utility Classes and Methods
- How to implement equals() and hashCode() methods in Java[reproduced]
- Java in Science: Data Interpolation and Extrapolation Using Numerical Methods of Polynomial Fittings
- Why Multiple Inheritance is Not Supported in Java
- Difference Between Class.ForName() And ClassLoader.LoadClass() Methods In Java
- [React] Define defaultProps and PropTypes as static methods in class component
- Methods for Using Java in ORACLE(转荐)
- Function Inheritance and Overriding and Function Overloading In D
- How to Test Private and Protected methods in .NET
- Previewing and plotting multiple sheets in AutoCAD using .NET
- The default keys and values of java system properties
- Programming Spiders, Bots, and Aggregators in Java by Jeff Heaton
- s1(java)项目实战(固定资产管理)(asset_InAndOut )
- Variable, Constant and Literal in Java