Hello, today I share Java abstract classes and interfaces with you. Take out your little book and write them down.
An abstract class
Abstract classes are simply declarations of methods in a class, with no implementation logic. Here is the syntax for abstract classes:
public abstract class Person {
public abstract void say();
Copy the code
}
It is easy to see the difference between ordinary class writing and the use of the abstract keyword in front of the class, indicating that Person is an abstract class. The second is that we use the abstract keyword when we declare the method and we declare the say method without any code logic.
Having said that, let’s talk briefly about the benefits of abstract classes. Above we define a Person class that has a say method. There are many subdivisions of human beings, such as doctors, students, etc., whose daily speech skills are different, so when inheriting the say method can be implemented (implement abstract classes, write the concrete logic of abstract classes), so as to meet their respective needs.
public abstract class Student extends Person {
}
Note that I wrote a student class to implement the say method. ‘Student’ must be declared as an abstract class, or implement the abstract method ‘say()’ in ‘Person’. OK, let’s implement this abstract method.
public class Student extends Person {
@override public void say() {system.out.println (" I am a student!" ); }Copy the code
}
Let’s write another doctor class
public class Doctor extends Person {
@override public void say() {system.out.println (" I am a doctor!" ); }Copy the code
}
Ok, two classes are written, let’s call it and see what happens.
At this point you might ask an abstract class do all methods have to be abstract? Of course not. It’s all abstract methods and that’s the interface, as we’ll see. Since not all methods have to be abstract, let’s declare a method to see what happens.
Here we add a new method to Person.
Okay, so that’s all about abstract classes, the syntax is pretty simple, but understanding the concept or understanding the idea of programming is really important. Finally, there are a few things to clarify when using abstract classes
-
Cannot be instantiated, that is, cannot create an object with new. Because abstract classes have unimplemented methods (abstract methods).
-
If a subclass inherits an abstract class, all the abstract methods must be implemented. If there are unimplemented abstract methods, then subclasses must also be defined as abstract classes. This is already mentioned in the example above.
-
Abstract classes cannot use final declarations. The final modifier indicates that the final class does not have subclasses, whereas an abstract class must have subclasses to be meaningful.
-
Abstract classes have constructors. Although we can’t execute a constructor on a new object ourselves, when the subclass object is instantiated, the abstract methods of the parent class are already implemented, so the JVM creates the parent object and calls the constructor (no-argument constructor) of the parent class. When we create a new subclass object, the superclass object is created first, so when we execute a constructor, the superclass constructor takes precedence.
interface
Let’s take a look at what interfaces are. I mentioned interfaces a little bit earlier when I said abstract classes. Maybe you already have a concept in your mind at this time.
If all the methods in a class are abstract methods and all the attributes are global constants, then the class can be defined as an interface.
The code implementation and syntax are simple, so focus on the meaning and benefits of interfaces.
In our daily work, a project has many functions, some of which will be reused, such as the function of data storage. At this time, if some places to use this function need to modify the logic of the data storage function how to do, affecting the whole body. If we abstract this function, we can solve this problem when we use it. Of course, you could argue that it would be too much trouble to reimplement every use, but that’s the price of reducing program coupling and making it easier to expand.
At work, the programs you write will face all kinds of challenges. No program is bug-free, and there will always be bugs (external factors that affect how your program works) without running them all the time. If you write a poorly extensible program, it’s hard to maintain when you don’t know how to fix it. So writing a program is like building a house, if you want to be stable and you want to be able to face all kinds of challenges, you have to be prepared at the beginning, and that preparation determines how high the house can go. Therefore, when writing programs, it is recommended that all functions are abstract methods, which use interfaces. For very large projects, it takes more than just an interface to make your program more extensible and less coupled.
Ok, so let’s see how it works.
public interface Person {
Public static final String info = "static final String info "; public void say();Copy the code
}
That’s easy. There is a problem here because interfaces themselves are made up of global constants and abstract methods, and abstract methods must be public modifiers, otherwise they cannot be inherited.
So the permission modifier public and the declaration key of the global constant before say can be removed. It goes like this:
public interface Person {
String info = "la la la la "; void say();Copy the code
}
Then use the interface to see the effect!
public class Student implements Person {
@override public void say() {system.out.println (" I am a student!" ); }Copy the code
}
Call to see the result
The end…
Interface also said, finally add some interface other use method, understand understand.
Implements the interface
The interface can be multiple:
Format:
Class subclass implements Parent interface 1, parent interface 2… {
}
The code above is called the implementation of the interface. If a class implements both an interface and an abstract class, it should be written in the following format:
Class subclass extends Parent class implements Parent 1, parent 2… {
}
Inheritance of interfaces
Interfaces allow multiple inheritance because they are abstract parts with no concrete implementation. For example:
interface C extends A,B{ }
conclusion
The difference between interfaces and abstract classes:
Abstract classes are inherited by subclasses, and interfaces are implemented by classes.
2. An interface can only declare abstract methods. Abstract classes can declare abstract methods and write non-abstract methods.
3. Variables defined in interfaces can only be public static constants (global constants). Variables in abstract classes are ordinary variables.
4. Abstract classes are used by inheritance, not multiple inheritance. Interfaces are used with implementations, which can be multiple implementations (note that one is inherited and one is implemented).
Static methods cannot be overridden by subclasses, so an interface cannot declare static methods.
Interfaces cannot have constructors, but abstract classes can. (Interface inheritance is different from class inheritance)
Well, that’s all for today’s article, hoping to help those of you who are confused on the screen