English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Today, I will talk about my understanding of abstract classes and interfaces in Java, including reference content:
1. Abstract Class
1Definition:
public abstract class Class{}
In Java, all objects are described using classes, but not all classes are used to describe objects. What I understand by abstract class is actually a high-level extraction of the common parts of the same type of things, which includes properties and behaviors. For example, cows, sheep, and pigs have common properties such as fur, and common behaviors such as lactation. Therefore, we can abstract the common parts into a mammal class, containing properties such as fur and behaviors such as lactation. When cows, sheep, and pigs inherit the mammal class, they also have the function of lactation. As for how to complete this function, it needs to be implemented by oneself.
2Characteristics
(1Classes modified by the Abstract keyword are abstract classes;
(2Classes modified by the Abstract keyword are abstract classes; and abstract methods must be public or protected, otherwise they cannot be inherited by subclasses. The default is public.
(3Abstract methods cannot have an implementation; otherwise, a compilation error will occur;
(4Abstract classes can define their own member variables and member methods;
(5When a subclass inherits an abstract class, it must implement all the abstract methods in the abstract class; otherwise, the subclass must also be defined as an abstract class;
(6Abstract classes cannot be instantiated.
3Verification whether the above provisions are indeed as described
This is a verification table I edited in Word, and I put it here as an image:
From the verification results shown in the figure above, all the theories are correct
Second, interface
1Definition:
public interface InterfaceName{}
An interface is used to provide methods. In my understanding, it is a high-level extraction of common behaviors of multiple classes, for example, all animals have common behaviors such as eating and sleeping. Then we can extract these two behaviors and encapsulate them in an interface. When an animal needs to perform this behavior, it only needs to call the interface and complete the specific implementation in its own class. This understanding seems to have no difference from abstract classes, let's see the following example. If these two behaviors are placed in an abstract class, but the abstract class also has a climbing behavior, then when a爬行动物, such as a snake, inherits this class, it will implement the three methods of eating, sleeping, and climbing, and thus it will have the functions of eating and sleeping; however, if a flying animal such as a bird inherits this method, it will also have the functions of eating, sleeping, and climbing, which is obviously not what it needs, which is a bit misleading. But when they inherit the interface with only eating and sleeping, they have the basic functions of eating and sleeping. As for climbing and flying, they can be abstracted out and placed in an abstract class, inherited as needed, and implemented with the functions they need.
2Characteristics:
(1The interface can have its own member variables, but they are implicitly specified as public static final, and can only be public static final. All methods in the interface are abstract methods, which are implicitly specified as public abstract.
(2) Interfaces only define abstract methods without specific implementations;
(3The class that implements the interface must implement all the methods defined in the interface;
3Verify whether the above theories are correct
Similarly, it is verified that the above theories are all correct!
Third, the differences between abstract classes and interfaces:
1Abstract classes can have their own member methods and their specific implementations; interfaces can only contain abstract methods;
2Abstract classes can contain various types of member variables; interface member variables can only be public static final;
3A class can only inherit one abstract class, but can implement multiple interfaces;
4Abstract classes can contain static code blocks and static methods; interfaces cannot define static code blocks and static methods;
Verify that a class can only inherit one abstract class, but can implement multiple interfaces
Firstly, define two abstract classes: one is the Mammals mammal class, and the other is the Crawler crawling class
/** * @createtime 2017Year3Month17Morning10:33:27 * @description Mammal class */ 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 2017Year3Month17Morning11:23:09 * @description Define an abstract class--Crawling 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 2017Year3Month17Morning11:03:42 * @description Define an interface named BasicAction */ public interface BaseAction { public String name = ""; public void eat(); public void sleep(); }
/** * @createtime 2017Year3Month17Morning11:24:56 * @description Define an interface to implement special behaviors */ public interface SpecialAction { public void study(); }
Then, define a basic class People, inheriting from the Mammals class, and implementing the BaseAction interface and SpecialAction interface
/** * @createtime 2017Year3Month17Morning11:25:48 * @description Define a common class--Humans, inherit mammals, implement basic interface and special interface */ 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 both the Mummals class and the Crawler class
/** * @createtime 2017Year3Month17Morning11:25:48 * @description Define a common class--Humans, inherit mammals, implement basic interface and special interface */ public class People extends Mammals, Crawler{ @Override public void nurse() { // TODO Auto-generated method stub } }
Summary
That is all about my personal understanding of Java abstract class and interface in this article. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Code example of bean inheritance and abstraction in Spring
Core idea of Hibernate and brief introduction of interface
Interface (interface) and usage example in Java
If there is anything lacking, please leave a message to point it out. Thank you friends for your support of this site!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)