multiple inhertance

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/85392295

java 8 example:

// interfaces/MultipleInheritance.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.*;

interface One {
  default void first() {
    System.out.println("first");
  }
}

interface Two {
  default void second() {
    System.out.println("second");
  }
}

interface Three {
  default void third() {
    System.out.println("third");
  }
}

class MI implements One, Two, Three {}

public class MultipleInheritance {
  public static void main(String[] args) {
    MI mi = new MI();
    mi.first();
    mi.second();
    mi.third();
  }
}
/* Output:
first
second
third
*/

It works find.

Java 7 example, change MultipleInheritance.java to MultipleInheritance0.java:

// interfaces/MultipleInheritance0.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.*;

interface One {
  void first();
}

interface Two {
  void second();
}

interface Three {
  void third();
}

class MI implements One, Two, Three {
  void first() { // [1]
    System.out.println("first");
  }

  void second() { // [2]
    System.out.println("second");
  }

  void third() { // [3]
    System.out.println("third");
  }
}

public class MultipleInheritance0 {
  public static void main(String[] args) {
    MI mi = new MI();
    mi.first();
    mi.second();
    mi.third();
  }
}

when compile this code use

$ javac -source 1.7  -target 1.7 interfaces/MultipleInheritance0.java

, the compile error is:

warning: [options] bootstrap class path not set in conjunction with -source 1.7
interfaces/MultipleInheritance0.java:21: error: first() in MI cannot implement first() in One
  void first() {
       ^
  attempting to assign weaker access privileges; was public
interfaces/MultipleInheritance0.java:25: error: second() in MI cannot implement second() in Two
  void second() {
       ^
  attempting to assign weaker access privileges; was public
interfaces/MultipleInheritance0.java:29: error: third() in MI cannot implement third() in Three
  void third() {
       ^
  attempting to assign weaker access privileges; was public

3 errors
1 warning

change [1], [2], [3] to the below code:

  public void first() {
    System.out.println("first");
  }

  public void second() {
    System.out.println("second");
  }

  public void third() {
    System.out.println("third");
  }

then compile the code, result is: 

warning: [options] bootstrap class path not set in conjunction with -source 1.7

1 warning

 after install jdk7, use

$ java -classpath . interfaces/MultipleInheritance0
// or
$ java interfaces/MultipleInheritance0

surprised, it shows error:

Exception in thread "main" java.lang.NoClassDefFoundError: interfaces/MultipleInheritanceOne (wrong name: MultipleInheritan
ceOne)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)

        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

 The solution is cd to interfaces directory, then use java command to execute the code. The result is:

/* My jdk7 Output:
first
second
third
*/

Java 8 example works find as long as all the base-class methods have distinc names and argument lists. If not, you get compile-time errors:

// interfaces/MICollision.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.*;

interface Bob1 {
  default void bob() {
    System.out.println("Bob1::bob");
  }
}

interface Bob2 {
  default void bob() {
    System.out.println("Bob2::bob");
  }
}

// class Bob implements Bob1, Bob2 {} // [1]
/* Produces:
error: class Bob inherits unrelated defaults
for bob() from types Bob1 and Bob2
class Bob implements Bob1, Bob2 {}
^
1 error
*/

interface Sam1 {
  default void sam() {
    System.out.println("Sam1::sam");
  }
}

interface Sam2 {
  default void sam(int i) {
    System.out.println(i * 2);
  }
}

// This works because the argument lists are distinct:
class Sam implements Sam1, Sam2 {}

interface Max1 {
  default void max() {
    System.out.println("Max1::max");
  }
}

interface Max2 {
  default int max() {
    return 47;
  }
}

// class Max implements Max1, Max2 {} // [2]
/* Produces:
error: types Max2 and Max1 are incompatible;
both define max(), but with unrelated return types
class Max implements Max1, Max2 {}
^
1 error
*/

when uncommend [1] and [2], my compile-time errors are: 

interfaces/MICollision.java:20: error: class Bob inherits unrelated defaults for bob() from types Bob1 and Bob2
class Bob implements Bob1, Bob2 {}
^
interfaces/MICollision.java:56: error: types Max2 and Max1 are incompatible; both define max(), but with unrelated return t
ypes
class Max implements Max1, Max2 {}
^

2 errors

when use javac command,  it generates Bob1.class and Bob2.class.

To fix the problem, you must override the conflicting method:

// interfaces/Jim.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.*;

interface Jim1 {
  default void jim() {
    System.out.println("Jim1::jim");
  }
}

interface Jim2 {
  default void jim() {
    System.out.println("Jim2::jim");
  }
}

public class Jim implements Jim1, Jim2 {
  @Override
  public void jim() {
    Jim2.super.jim();
  }

  public static void main(String[] args) {
    new Jim().jim();
  }
}
/* Output:
Jim2::jim
*/

referecnes:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/MultipleInheritance.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/MICollision.java

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/Jim.java

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/85392295
今日推荐