1. abstract class
Definition
It doesn’t create objects directly , You create classes of objects only by inheriting subclasses ( A template ) Call it abstract class . Its definition and rules are as follows :
- There doesn’t have to be an abstract method in an abstract class , But the class containing abstract method must be abstract class or interface
- Abstract classes need to use
abstract
Keyword declaration - Methods in abstract classes only declare , Don’t realize
- Abstract classes must have subclasses , And subclasses must override all abstract methods .
- Abstract classes can contain Field, Method , Constructors , Initialization block , Inner class , There are six kinds of components
Be careful
abstract
Cannot coexist with the following keywords
static
Class name . Abstract method name (); It doesn’t make any sensefinal
After retouching , Abstract methods cannot be overriddenprivate
The purpose of abstract methods is to make subclasses re implement , After retouching , Subclass not visible .
public abstract class Test {
private String name;
private int age;
public Test() {
}
public Test(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 void printName(String name) {
System.out.println("name: " + name);
}
// Abstract methods in abstract classes
public abstract void printAge();
}
explain : There are not too many differences between abstract classes and ordinary classes , Just define one or more abstract methods when defining a class , Except that you can’t instantiate objects directly , The rest is no different from the general class .
2. Interface (interface)
An interface can be thought of as a special class , It consists of global constants and public
Defined abstract methods are composed of ( The default abstract method is public
jurisdiction ), Interface to use interface
Keyword declaration , It produces a completely abstract class ( More abstract than abstract classes ), No specific implementation is provided .
The interface definition can be as follows :
public interface InterfaceDemo {
public final static String name = "FEEL";
public void eat();
public void sleep();
}
And for the use of interfaces , We use implements
Keyword to implement the interface , This is different from inheritance .
Specific reference is as follows :
public class Test implements InterfaceDemo{
public static void main(String[] args) {
System.out.println(name);
Test test=new Test();
test.eat();
test.sleep();
}
@Override
public void eat() {
System.out.println("FEEL Eating snacks ");
}
@Override
public void sleep() {
System.out.println("FEEL At rest ");
}
}
The interface has the following features
- Methods in interfaces can have parameter lists and return types , But there can be no methodology .
- The interface can contain fields , But is implicitly declared as
static
andfinal
. - The fields in the interface are only stored in the Interface static storage area , It doesn’t belong to the interface .
- Methods in an interface can be declared as
public
Do not declare , Defaultpublic
Handle - When implementing an interface , You need to declare the defined method as
public
type - If you want to implement all the methods in the interface , The current creation is still an interface
- Extend an interface to generate a new interface to use
extends
keyword , Implement interface usageimplements
keyword - The method in the interface is abstract (
abstract
) Of , It can’t be static (static
) Of , The methods in the interface are all abstract , And abstract methods don’tstatic
, Because there isstatic
The abstract method of cannot be overridden , That’s the only way , Interfaces make sense .
review : A class can inherit an abstract class , And implement multiple interfaces . About abstract classes and interfaces
3. The difference between abstract classes and interfaces
- An interface is a variant of an abstract class , All methods in the interface are abstract , The abstract class is just a kind of method that can be declared and not implemented .
- Interfaces can inherit more , You can’t abstract classes
- The method defined in the interface is not implementable , And the part of the abstract class can implement
- The basic data type in the interface must be
static
Modified , And abstract classes are not - Interfaces cannot contain static code blocks and static methods , And abstract classes can contain
in addition , Abstract classes are more functional than interfaces , But defining abstract classes is expensive . Although the interface is a little poor in function , But it’s just a description of an action , And you can implement multiple interfaces in a class , therefore , It helps to reduce the complexity of design .