0X00 abstract class & Interface Introduction
Abstract class
Abstract abstract modifiers--abstractions are designed to integrate subclasses and not directly implement an object
All abstract methods in an abstract class are implemented in subclasses
Classes that have abstract methods must be declared as abstract classes
Abstract classes can have methods that are not abstract
Interface
Interface interface modifiers--interfaces are designed to enable classes to implement the
The variable defaults to public static final and cannot be changed
method is public abstract by default and cannot be changed
Interface does not implement methods
0X01 the difference between an abstract class and an interface
Abstract classes can implement method details, and interfaces cannot
The variables of an abstract class can be of various types, and interfaces cannot be
Abstract classes can have static code blocks and static methods, and interfaces cannot
A class can implement multiple interfaces and can only inherit from an abstract class
Inheritance can be understood as "is not", the interface can be understood as "there"
0X02, for instance,
There is an interface canfly
Public interface Canfly {
public abstract void Fly ();
}
There is an abstract class Bird
Public abstract class Bird {
int age;
void Eat () {
System.out.println ("I can Eat insect~");
}
}
There is a Sparrow class that inherits from Bird
public class Sparrow extends Bird implements canfly{
public void Fly () {
System.out.println ("I Can Fly");
}
}
There is an abstract class airplane
Public abstract class Airplane {
Double price;//Price
void Crash () {//crash
System.out.println ("This airplane is crashed!");
}
}
There is a jian_10 class that inherits from airplane
public class Jian_10 extends airplane implements canfly{
public void Fly () {
System.out.println ("I Can Fly");
}
}
There is a class that contains the main method to test
public class Main {
public static void Main (string[] args) {
Jian_10 a_0 = new jian_10 ();//Instantiate A_0 fighter
Sparrow xiaoming = new Sparrow ();/Yes, this sparrow is called xiaoming.
We can all fly.
A_0.fly ();
Xiaoming.fly ();
Xiaoming eats.
Xiaoming.eat ();
The fighter crashed.
A_0.crash ();
}
}
The result of the run is this
I can Fly
I can Fly
I can eat insect~
This airplane is crashed!
0x03 a rough explanation.
That's probably what happened:
Airplane and Bird are two abstract classes, and Jian_10 and Sparrow inherit from them respectively, so subclasses can call the parent class's methods directly. and Jian_10 and Sparrow also have interface canfly. Then jian_10 and Sparrow implement the Fly method declared in the interface Canfly (must be implemented).
If you later want to modify the methods of airplane and Bird two parent classes, such as I do not want to let Bird eat or airplane will not crash, only need to modify the airplane and Bird the corresponding method.
A class can only inherit from a class & abstract class, but it is possible to implement multiple interfaces
For example, airplane and Bird have many of the same methods, but the implementation is not the same, we can put these methods into an interface.