Today I will talk about my own understanding of abstract classes and interfaces in Java, including reference content:
1. Abstract Class
1. Definition:
public abstract class class name {}
All objects in the Java language are described by classes, but not all classes are used to describe objects. The abstract class I understand is actually a high degree of extraction of the public parts of the same type of things, which include attributes and behaviors. For example, cattle, sheep, and pigs have all their public attributes, and public behaviors are all breastfeeding, so we can abstract the public part into a mammal, containing attributes, hair and behaviors. When cattle, sheep, and pigs inherit the mammal, they also have the function of breastfeeding. As for how to complete this function, they need to implement it themselves.
2. Features
(1) The class modified by the Abstract keyword is an abstract class;
(2) Classes containing abstract methods must be abstract classes, but abstract classes do not necessarily contain abstract methods; and abstract methods must be public or protected, otherwise they cannot be inherited by subclasses. Default is public.
(3) There cannot be implementation in the abstract method, otherwise the compilation will be reported;
(4) You can define your own member variables and member methods in an abstract class;
(5) When a subclass inherits an abstract class, all abstract methods in the abstract class must be implemented, otherwise the subclass must also be defined as an abstract class;
(6) Abstract classes cannot be instantiated.
3. Verify whether the above provisions are indeed as stated
This is a verification table I edited in word, and I cut it into an image and put it here:
Judging from the verification results in the figure above, those theories are correct
2. Interface
1. Definition:
public interface interface name {}
The interface is used to provide methods. According to my understanding, it is a high-level extraction of public behaviors of multiple classes. For example, the public behavior of all animals is eating and sleeping. Then we can extract and encapsulate these two behaviors in one interface. When an animal needs to perform this behavior, just call the interface and then complete the specific implementation in its own class. This understanding seems to be no different from abstract classes, so let’s take a look at the following example. If these two behaviors are placed in an abstract class, but there is also a crawling behavior in the abstract class. At this time, when a reptile, such as a snake, inherits this class, it will realize the three methods of eating, sleeping, and crawling, so it has the functions of eating, sleeping, and crawling; but if an animal of a flying class is like a bird, when it inherits this method, it also has the functions of eating, sleeping, and crawling. It is obvious that crawling is not the function it needs, which is a bit inconsistency. However, when they inherit only the interface of eating and sleeping, they have the basic functions of eating and sleeping. As for climbing and flying, they can be abstracted and placed in an abstract class, inherit on demand, and realize the functions they need on demand.
2. Features:
(1) The interface can have its own member variables, but they will be implicitly specified as public static final, and they can only be public static final. All methods in the interface are abstract methods and will be implicitly specified as public abstract.
(2) Only abstract methods are defined in the interface, and there is no concrete implementation;
(3) The class that implements an interface must implement all methods defined in the interface;
3. Verify whether the above theory is correct
Similarly, the above theories are all correct!
3. The difference between abstract classes and interfaces:
1. The abstract class can have its own member methods and their specific implementations; the interface can only contain abstract methods;
2. Abstract classes can contain various types of member variables; member variables in interfaces can only be public static final;
3. A class can only inherit one abstract class, but can implement multiple interfaces;
4. Abstract classes can contain static code blocks and static methods; static code blocks and static methods cannot be defined in the interface;
Verify that a class can only inherit one abstract class, but can implement multiple interfaces
First, define two abstract classes: a Mummals mammal and a Crawler reptile class
/** * @createtime March 17, 2017 at 10:33:27 am * @description Mammals */public abstract class Mammals {public String foods;public abstract void nurse();public void eat(String food){this.foods = food;System.out.println("eat"+foods);}} /** * * @createtime March 17, 2017 at 11:23:09 am * @description Define an abstract class - crawler class */public abstract class Crawler {public abstract void crawl();}Secondly, define two interfaces: one is the BaseAction basic interface; the other is the SpecialAction special interface
/** * * @createtime March 17, 2017 at 11:03:42 am * @description Define an interface named Basic behavior */public interface BaseAction {public String name = "";public void eat();public void sleep();} /** * @createtime March 17, 2017 at 11:24:56 am * @description Define an interface to implement special behavior*/public interface SpecialAction { public void study();}Then, define a basic class People, inherit the Mummals class, and implement the BaseAction interface and SpecialAction interface
/** * @createtime March 17, 2017 at 11:25:48 am * @description Define a common class - human, inherit mammals, implement basic interfaces and special interfaces */public class People extends Mammals implements BaseAction,SpecialAction{@Override public void study() {// TODO Auto-generated method stub}@Override public void eat() {// TODO Auto-generated method stub}@Override public void sleep() {// TODO Auto-generated method stub}@Override public void nurse() {// TODO Auto-generated method stub}}It can be seen that a subclass can implement multiple interfaces.
Finally, let the basic class People inherit the Mummals class and Crawler class at the same time
/** * @createtime March 17, 2017 at 11:25:48 am * @description Define a common class - human, inherit mammals, implement basic interfaces and special interfaces */public class People extends Mammals,Crawler{@Override public void nurse() {// TODO Auto-generated method stub}}Summarize
The above is all the content of this article about briefly discussing the personal understanding of Java abstract classes and interfaces. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Examples of inheritance and abstract code of beans in Spring
Introduction to Hibernate's core ideas and interfaces
Examples of interface and usage methods in Java
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!