Learning objectives:
Understand Java constants, interface multiple implementations, multiple inheritance, and permission modifiersLearning content:
1. Constants in Java
2. Multiple implementation of interface
3. Multiple inheritance of interfaces
4. Permission modifier
Learning steps
1. Constants in Java
In the interface, member variables can be defined, but they must be decorated with the keyword public static final
In effect, it is equivalent to a constant
1. The constants in the interface are decorated with public static final. These three keywords can be omitted
2. It is equivalent to a constant, so it can only be assigned during initialization and cannot be changed later
3. It is recommended that the naming format be underlined with capital letters
Example 1:
package com.hg.day13.demo01; public class Demo01Interface { int num = 5; public void method() { } }
package com.hg.day13.demo01; public interface MyInterfaceConst { public static final int NUM_OF_CLASS=5; }
2. Multiple implementation of interface
1. There are no static code blocks and construction methods in the interface
2. There is only one direct parent class of a class, but a class can implement multiple interfaces
3. If there are duplicate abstract methods in multiple interfaces implemented by the implementation class, it only needs to be overwritten and rewritten once
4. If the implementation class does not override all abstract methods in all interfaces, the implementation class must be an abstract class
5. If there are duplicate default methods in multiple interfaces implemented by the implementation class, the implementation class must override the default methods
6. If the method in the direct parent class of a class conflicts with the default method in the interface, the method in the parent class shall prevail
Example 2:
package com.hg.day13.demo02; public class Demo02Interface { public static void main(String[] args) { Zi zi=new Zi(); zi.method(); } }
package com.hg.day13.demo02; public interface MyInterface { public default void method() { System.out.println("I am the default method in the interface"); } }
package com.hg.day13.demo02; public interface MyInterfaceA { //Error: static code block is not allowed in the write method interface //static {} //Error: there cannot be a constructor in the write method interface //public MyInterfaceA() public abstract void methodA(); public abstract void methodAbs(); public default void methodDefault() { System.out.println("Default method AAA"); } }
package com.hg.day13.demo02; public interface MyInterfaceB { public abstract void methodB(); public abstract void methodAbs(); public default void methodDefault() { System.out.println("Default method BBB"); } }
package com.hg.day13.demo02; public abstract class MyInterfaceAbs implements MyInterfaceB,MyInterfaceA{ @Override public void methodB() { } @Override public void methodAbs() { } @Override public void methodDefault(){ MyInterfaceA.super.methodDefault(); } }
package com.hg.day13.demo02; public class MyInterfaceImpl implements MyInterfaceA,MyInterfaceB{ @Override public void methodB() { } @Override public void methodA() { } //Just write it once @Override public void methodAbs() { } @Override public void methodDefault(){ MyInterfaceA.super.methodDefault(); } }
package com.hg.day13.demo02; public class Fu { public void method() { System.out.println("I am the method of the parent class"); } }
package com.hg.day13.demo02; public class Zi extends Fu implements MyInterface{ }
3. Multiple inheritance of interfaces
1. Class and class are single inheritance, and there is only one direct parent class
2. Between classes and interfaces, one class can implement multiple interfaces
3. There is multiple inheritance between interfaces
be careful:
1. It doesn't matter if the abstract methods in multiple parent interfaces are repeated
2. Duplicate default method in multiple parent interfaces
Example 3:
package com.hg.day13.demo03; import com.hg.day13.demo02.MyInterface; public class Demo03Relations { //MyInterface myinterface=new MyInterface(); Wrong writing }
package com.hg.day13.demo03; /* There are four methods in this interface methodA(Interface A method) methodB (interface B method) methodcommon (interface AB method) method (own method) */ public interface MyInterface extends MyInterfaceA,MyInterfaceB{ public abstract void method(); @Override default void methodDefault(){ MyInterfaceA.super.methodDefault(); } }
package com.hg.day13.demo03; public interface MyInterfaceA { public abstract void methodA(); public abstract void methodCommon(); public default void methodDefault() { System.out.println("AAAAA"); } }
package com.hg.day13.demo03; public interface MyInterfaceB { public abstract void methodB(); public abstract void methodCommon(); public default void methodDefault() { System.out.println("BBBBB"); } }
package com.hg.day13.demo03; //import com.hg.day13.demo02.MyInterface; public class MyInterfaceImpl implements MyInterface { @Override public void methodA(){ } @Override public void methodB(){ } @Override public void method(){ } @Override public void methodCommon(){ } @Override public void methodDefault(){ } }
4 permission modifiers in Java
package com.hg.day13.demo04; /* java There are four permission modifiers in public protected default private In the same class (myself) yes yes yes In the same bag (my neighbor) yes yes no Subclass (son) in the same package yes yes no Subclasses (sons) in different packages Yes No Non subclasses of different packages Yes No */ public class Demo01Main { }
Example 4:
package com.hg.day13.demo04; public class MyAnother { public void anotherMthode() { MyClass myclass =new MyClass() ; System.out.println(myclass.num); } }
package com.hg.day13.demo04; public class MyClass { public int num =5; public void method() { System.out.println(num); } int sum=num+1; }
package com.hg.day13.demo04; public class MySon1 extends MyClass{ public void method() { //System.out.println(super.num); } }
package com.hg.day13.demo05; import com.hg.day13.demo04.MyClass; public class MySon2 extends MyClass { public void method(){ //System.out.println(super.num); } }
package com.hg.day13.demo05; import com.hg.day13.demo04.MyClass; public class Stranger { public static void main(String[] args) { MyClass myclass=new MyClass(); } }