1, Why use interfaces?
1.Door.java
public abstract class Door { // public Door(){ // // } //Open the door public abstract void openDoor(); //close public abstract void closeDoor(); // public static void main(String[] args) { // Door door = new Door(); // } }
2.DoorBell.java
public interface DoorBell { //Photo storage public abstract void photo(); }
Why do I need an interface? What is the difference between interfaces and abstract classes?
- an interface is an "abstract class" that is more "abstract" than an "abstract class", which can restrict subclasses more formally.
Comprehensively and professionally realized the separation of specification and concrete implementation.
- an interface is a specification, which defines a set of rules, reflecting the real world "if you are... You must"
Can... "Thought. If you are an angel, you must be able to fly. If you are a car, you must be able to run. If you are a good man, you must kill the bad
People; If you are a bad person, you must bully the good person.
– the essence of interface is contract, just like the law between us. After making it, everyone will abide by it.
– the specific needs of the project are changeable, and we must respond to changes with constancy in order to develop calmly. Here
"Invariance" is "norm". Therefore, our development projects are often interface oriented programming!
Characteristics of interface:
- the interface cannot be instantiated
– the implementation class must implement all methods of the interface
– implementation classes can implement multiple interfaces
- all variables in the interface are static constants
Interface related rules
- all methods in the interface are abstract.
– even if the members in the interface are not explicitly marked with public, they are of public access type
– the variables in the interface are marked with public static final by default, so the variables defined in the interface are all variables
Local static constant.
– you can define a new interface and use extensions to inherit an existing interface
- you can define a class and use implements to implement all methods in an interface.
- you can define an abstract class and use implements to implement some methods in an interface.
2, Interface oriented programming example:
1.Lock.java
public interface Lock { public static final int num = 100; // public Lock(){ // // } //Unlock public abstract void openLock(); //Lock public abstract void closeLock(); }
2.LockDoor.java
public /*abstract*/ class LockDoor extends Door implements Lock,DoorBell/*,Runnable,Serializable*/ { @Override public void openDoor() { System.out.println("Open the door"); } @Override public void closeDoor() { System.out.println("close"); } @Override public void openLock() { System.out.println("Unlock"); } @Override public void closeLock() { System.out.println("Lock"); } @Override public void photo() { System.out.println("Click click... ...The picture is stored"); } // @Override // public void run() { // // } }
3.TestLockDoor.java
public class TestLockDoor { public static void main(String[] args) { LockDoor lockDoor = new LockDoor(); lockDoor.openLock(); lockDoor.openDoor(); lockDoor.closeDoor(); lockDoor.closeLock(); lockDoor.photo(); // Lock lock = new LockDoor(); // System.out.println(lock.num); // Lock lock = new Lock(); // Lock.num = 200; // System.out.println(Lock.num); } }
4.DoorBell.java
public interface DoorBell { //Photo storage public abstract void photo(); }
In java, a subclass can only have one parent class, which is inherited by a single class. If a class needs to inherit multiple classes, we need to consider using interface implementation.
In the actual project development process, the interface is widely used.
Usage:
1. The interface needs to be defined with interface
2. If a class implements an interface, all abstract methods in the interface must be implemented
3. Implement the interface with implements
Features:
1. All methods in the interface are modified by public abstract, that is, the default modifier. Whether written or not, they are public abstract,
When the modifier public is omitted, it cannot be understood as the default modifier used. It is still public and can only be public
2. An interface is a special abstract class. An interface cannot be instantiated. This feature is the same as an abstract class, but it does not mean that there can be no constructor,
There can be constructors in abstract classes, but not in interfaces. As we said before, as long as an object can be created through the new keyword, it will be found
A corresponding constructor is executed, but the opposite is not necessarily true. That is to say, a constructor does not necessarily create an object,
However, if new an object, there must be a corresponding constructor
3. The implementation class must implement all the methods in the interface, but if you want to implement some methods in the interface and some are not implemented, you need to implement this implementation class
It can be defined as an abstract class
4. The implementation class can implement multiple interfaces
5. All variables in the interface are static variables. The default modifier is public static final, and the modifier is public whether to write or not,
And it can only be public
6. One interface can inherit multiple interfaces
Note:
1. Interface refers to a capability, which is embodied in the method of interface
2. Care about the ability of the implementation class, but not the implementation details. The interface oriented Convention does not consider the specific implementation of the interface
3. Interface is an agreement
High frequency interview questions in java foundation stage:
What is the difference between an interface and an abstract class?
1. Interface is a special abstract class;
2. Implement an extension class before implementing the extensions class;
3. A class can only inherit one abstract class, but can implement multiple interfaces;
4. Abstract classes and interfaces cannot be instantiated, but there can be constructors in abstract classes, but not in interfaces;
5. Abstract classes can have variables or constants, method implementations or method definitions, while interfaces can only have global static constants,
The default modifier is public static final, which cannot have variables and can only have method definitions. The default modifier is public abstract,
There can be no method of implementation;
6. The inheritance between classes is single inheritance, while the inheritance between interfaces is multi inheritance
3, Inner class
Defining a class inside another class is called an inner class
Member inner class:
matters needing attention:
▪ (1) External classes cannot directly use members and methods of internal classes
▪ (2) If the external class and the internal class have the same member variable method, the internal class accesses its own member variable method by default. If you want to access the member variable of the external class, you need to use this keyword
Access internal classes externally:
grammar
▪ External class external class object = new external class ();
▪ External class Inner class inner class object = outer class object new inner class ();
If the main method is inside an external class, Outer can be omitted
Static inner class
grammar
▪ new external class name Inner class () Method name
▪ External class name Internal class internal class object name = new external class name Internal class name ();
Internal classes declared with static cannot access external properties that are not static
Anonymous Inner Class
It is suitable for classes that only need to be used once, and more are used in Android
Anonymous inner class anonymous
– an interface can be implemented, and the abstainer inherits a parent class
– only one interface can be implemented
– it is suitable for creating classes that only need to be used once and cannot be reused. It is commonly used in graphical interface programming GUI.
– if anonymous inner classes want to use local variables of outer classes, they must use final to modify the local variables
Method inner class
Method inner class refers to defining the inner class in the method of the outer class.
▪ matters needing attention:
▪ Method internal classes cannot be used outside the methods of external classes, so method internal classes cannot use access control characters and static modifiers
Internal class summary:
Putting one class definition into another is called "inner class"
▪ Internal class characteristics defined in class
as a member of the external class, the internal class can directly access the members of the external class (including private members), otherwise it cannot.
as members of external classes, internal classes can be declared as private, default, protected and public.
internal class members are valid only within the scope of internal class.
define the inaccessible attributes in the external class with the internal class. In this way, the implementation in the external class is smaller than the private of the external class
Access rights for.
– generate two classes after compilation: outerclass Class and outerclass $innerclass class
▪ Internal class classification
– member inner class static inner class method inner class anonymous inner class
OuterClass.java
public class OuterClass{ private String name = "Fei Zhang"; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public void show(){ System.out.println("This is an external class show method"); // System.out.println(gender); // test(); //create a class object // InnerClass innerClass = new InnerClass(); // System.out.println(innerClass.gender); // innerClass.test(); } //Member inner class class InnerClass{ private String gender; private String name = "Zhao Yun"; // private static String no; public void test(){ System.out.println("This is the name of the inner class test method"); // System.out.println(gender); // System.out.println(name); // System.out.println(id); // show(); System.out.println(name); System.out.println(this.name); System.out.println(OuterClass.this.name); // OuterClass outerClass = new OuterClass(); // outerClass.show(); } class InnerInnerClass{ public void test1(){ System.out.println("This is the inner class in the inner class"); } } } //Main method / entrance method // public static void main(String[] args) { // InnerClass innerClass = new OuterClass().new InnerClass(); // } }
TestOuterClass.java
public class OuterClass{ private String name = "Fei Zhang"; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public void show(){ System.out.println("This is an external class show method"); // System.out.println(gender); // test(); //create a class object // InnerClass innerClass = new InnerClass(); // System.out.println(innerClass.gender); // innerClass.test(); } //Member inner class class InnerClass{ private String gender; private String name = "Zhao Yun"; // private static String no; public void test(){ System.out.println("This is the name of the inner class test method"); // System.out.println(gender); // System.out.println(name); // System.out.println(id); // show(); System.out.println(name); System.out.println(this.name); System.out.println(OuterClass.this.name); // OuterClass outerClass = new OuterClass(); // outerClass.show(); } class InnerInnerClass{ public void test1(){ System.out.println("This is the inner class in the inner class"); } } } //Main method / entrance method // public static void main(String[] args) { // InnerClass innerClass = new OuterClass().new InnerClass(); // } }