1. interface: interfance interface name {} How the interface is implemented: The interface cannot be instantiated directly. Another keyword is required for other classes to implement the interface: implements It is implemented in the form of polymorphism and instantiated by specific subclasses. In fact, this is a kind of polymorphism, which is called interface polymorphism 1) Member variables in the interface can only be constants, with the modifier public static final by default 2) The member method in the interface is also a public abstract with a default modifier 3) There is no constructor in the interface 4) To instantiate an interface, create a concrete class to implement the interface and implement all the abstract methods in the interface 5) There is an inheritance relationship between interfaces. Single inheritance or multiple inheritance can be performed 6) If a concrete class implements an interface, all abstract methods in the interface must be implemented If an abstract class implements the interface, it can implement the method selectively or not
Characteristics of members in the interface: Member variables: Can only be constant and static The JVM will automatically fill in the modifier: public static final before running Construction method: There is no constructor in the interface Member method: Methods in an interface can only be abstract methods, without method body or curly braces The JVM will automatically fill in the modifier: public abstract before running be careful: 1. When a concrete class implements an interface, it must implement all abstract methods in the interface 2. When an abstract class implements an interface, it can choose not to implement the methods in the interface or selectively
Class to class, class to interface, interface to interface relationship Class and class: There is an inheritance relationship. Only single inheritance can be performed. Multiple inheritance is not allowed, but multi-level inheritance can be performed Classes and interfaces: The implementation relationship can be a single implementation, multiple interfaces can be implemented at one time, or multiple interfaces can be implemented while inheriting a class Interface and interface: There is an inheritance relationship, which can be single inheritance or multiple inheritance
2. When referencing data type as formal parameter When a class is used as a parameter type, what is actually needed is the address value of the object of the class When an abstract class is used as a parameter type, what is actually needed is the address value of the object of the subclass of the abstract class When an interface is used as a type of formal parameter, what is actually needed is the address value of the object of the class implementing the interface 3. When referencing a data type as the return value of a method When a class is used as the return value type of a method, what is actually needed is the object of the class When an abstract class is used as the return value of a method, what is actually needed is the object of the subclass of the abstract class When an interface is used as the return value of a method, what is actually needed is the object of the specific class that implements the interface 4. Order of package import class 5. Access modifier Permission modifier: under the same class, in the same package subclass, other classes, subclasses of different packages, and other classes of different packages public √ √ √ √ protected √ √ √ Default √ private √
Class and its composition can use modifiers:
Modifier:
Access rights modification: public,protected, default, private
Status modifier: static,final
abstract modifier: abstract
Class:
Access permission modification: public, default,
Status decoration: final
abstract modifier: abstract
Member variables:
Access rights modification: public,protected, default, private
Status modifier: static,final
Construction method:
Access rights modification: public,protected, default, private
Member method:
Access rights modification: public,protected, default, private
Status modifier: static,final
abstract modifier: abstract
Common modifier combinations:
Member variable: public static final is encountered in the interface
Member method:
1,public abstract
2,public static
3,public final
(1) In the future, when an abstract class is used as a parameter type, what is actually needed is the address value of the object implemented by the subclass of this class, which is created in the form of polymorphism.
(2) In the future, when you see that the return value of a method is an abstract class type, you need to return the subclass object of the abstract class.
(3) In the future, when you see that the parameter of a method is a class type for parameter transfer, you will actually upload the address value of the class object
(4) In the future, when the return value of a method is the type of a class, it actually returns the address value of the class object
(5) When you see that the parameter type of a method is an interface, what you actually need is the address value of the implementation class object of the interface, which is created in the way of interface polymorphism
(6) When an interface is used as a return value type, what is needed is the object of the class that implements the interface
Interface test: (cat drill fire ring)
//Cat drill fire ring abstract class Animal{ private String name; private int age; public Animal(){} public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public abstract void eat(); } class Cat extends Animal{ public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Cats eat fish"); } } interface ZuanHuoQuan{ void zuanHuo(); } class TrainCat extends Cat implements ZuanHuoQuan{ @Override public void zuanHuo() { System.out.println("Cat drill fire ring"); } } public class InterfanceDemo1 { public static void main(String[] args) { Cat cat = new TrainCat(); TrainCat trainCat =(TrainCat) cat; trainCat.zuanHuo(); ((TrainCat) cat).zuanHuo(); } }
Cat and dog case: (add the function of high jump)
/* Cat and dog case: add the function of high jump analysis: Cat: Attributes: name, age Behavior: eat, sleep dog: Attributes: name, age Behavior: eat, sleep Because cats and dogs have something in common, we extract them into a new class Animals: (abstract class) Attributes: name, age Behavior: eating (), sleeping () Cat: inherited from animals Dogs: inherited from animals Since the high jump is an additional function, not unique to animals, we use the interface to define it Interface: high jump Partial cat: implementation interface Partial dog: implementation interface */ abstract class Animal1{ private String name; private int age; public void Animal1(){} public Animal1(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public abstract void eat(); public abstract void sleep(); } class cat extends Animal1{ public void cat(){} public cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Cats eat fish"); } @Override public void sleep() { System.out.println("The cat curled up to sleep"); } } class dog extends Animal1 { public void dog(){} public dog(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Dogs eat meat"); } @Override public void sleep() { System.out.println("The dog slept on its stomach"); } } interface Jump{ public abstract void jump(); } class JumpCat extends Cat implements Jump{ public void JumpCat(){} public JumpCat(String name,int age){ super(name, age); } @Override public void jump() { System.out.println("Cat high jump"); } } class JumpDog extends dog implements Jump{ public void JumpDog(){} public JumpDog(String name, int age) { super(name, age); } @Override public void jump() { System.out.println("Dog high jump"); } } public class InterfanceDemo3 { public static void main(String[] args) { Animal1 animal1 = new dog("Xiaobai",2); animal1.eat(); animal1.sleep(); dog dog = new JumpDog("Big white",3); ((JumpDog) dog) .jump(); } }