abstract classes and abstract methods
Abstract MethodsAbstract methods in object-oriented programming languages refer to methods that have only method declarations and no concrete method bodies. Abstract methods generally exist in abstract classes or interfaces. In Java, the abstract method is toAbstractThe method of modification,only need to declare and do not need to implementAbstractvoidf ();
Abstract class
Classes that contain abstract methods are called abstract classes.
AbstractclassSample {
Private intparam1;
AbstractvoidF1 ();
voidF2 () {}
}Note:
1. If a class contains an abstract method, it must be qualified as an abstract class, or the compiler will give an error.
2, abstract class can not be instantiated, or the compiler will error.
Interfacewhat is an interfaceIn Java, an interface is aInterfaceTo decorate.
An interface can be treated as a class that is more abstract than an "abstract class".
Implementing interfaces
A class follows a specific or set of interfaces and must use the implements keyword.
InterfaceFly {
voidFly ();
}
classBirdImplementsFly {
@Override
PublicvoidFly () {
System.out.println ("Wings Fly");
}
}
Note: the method in the interface must be of the public type, and can be specified without display. However, if you specify a different type, the compiler will give an error.
Expansion interface
Friends who have played games must be familiar with this pattern.
A warrior needs to be capable of fighting. A knight needs not only combat skills but also riding.
The more advanced Paladin also needs the Mage's magical abilities. This relationship can be abstracted into the following code:
InterfaceWarrior {
voidFight ();
}
InterfaceKnightextendsWarrior {
voidRiding ();
}
InterfaceEnchanter {
voidMagic ();
}
//The Paladin class implements multiple interfaces and requires all methods that contain these interfaces
classPaladinImplementsKnight, Enchanter {
PublicvoidFight () {}
PublicvoidRiding () {}
PublicvoidMagic () {}
}
Multiple inheritance
Consider, for example, an in-service graduate student who is both a worker, a student and a graduate student. What do we do if we want to implement this inheritance relationship in our code?
Multiple inheritance is supported in C + +, butmultiple inheritance is not supported in J Ava , which is incorrect in the following ways. A class cannot extend multiple classes.
classperson {};
classStudentextendsperson {};
classPostgraduateextendsStudent {};
classEmployeeextendsperson {};
classOnjobpostgraduateextendsPostgraduate, Employee {};
Although multiple inheritance is not supported in Java, similar behavior can be achieved through an interface. A class can implement multiple interfaces.
Interfaceperson {};
InterfaceStudentextendsperson {};
InterfacePostgraduateextendsStudent {};
InterfaceEmployeeextendsperson {};
classOnjobpostgraduateImplementsPostgraduate, Employee {};
JAVA Basic Interface