Abstract classes and Interfaces I. Abstract
1. Abstract class
Java can define a number of methods without a method body, its method body to the subclass of the class to implement according to their own situation, such a method is an abstract method, the class containing the abstract method is called abstract class. There can be one or more abstract methods in an abstract class.
Abstract precautions must be defined with the abstract modifier, and any class with an abstract method must be declared as an abstract class.
(1) Abstract class definition rules
A. Abstract classes must be modified with the abstract keyword, and abstract methods must be modified with abstract;
B. Abstract classes cannot be instantiated, that is, you cannot use the new keyword to produce objects;
C. Abstract methods only need to be declared, but not implemented;
D. A class containing abstract methods must be declared as an abstract class, and the subclass of the abstract class must overwrite all abstract methods before it can be instantiated, or the subclass is an abstract class.
2. How to do abstract methods
Abstract method (parameter list) for return value types in abstract;
3. Examples of abstract classes and abstract methods
1 Abstract class abs{2 Abstract int aa ( int x,int y); 3 }
Note: Classes that contain abstract methods are definitely abstract classes, and an abstract method in an abstract class is not necessarily abstract.
Second, interface (interface)
1. Interface
If all the methods in an abstract class are abstract, the class can be defined in a different way, that is, the interface definition. An interface is a collection of definitions of abstract methods and constant values, in essence, an interface is a special abstract class, in which the abstract class contains only the definitions of constants and methods, without the implementation of variables and methods.
2. Examples of interface definitions
1 Interface runner{2 int ID = 1; 3 void run (); 4 }
In the definition of interface runner, members are not explicitly identified with the Public keyword in a timely manner, but these members are of the public access type. The variables in the interface are identified by default with public static final, so the interface defines the global variables.
We can define a new interface, use the extends keyword to inherit an existing interface, or you can define a class that uses the Implements keyword to implement all the methods in an interface, and you can define an abstract class. Use the Implements keyword to implement a partial method defined in a connector.
1 //define an interface2 Interfacerunner{3 intID = 1;4 voidrun ();5 }6 //You can define a new interface and use the extends keyword to inherit an existing interface .7 InterfaceAnimalextendsrunner{8 voidBreathe ();9 }Ten //you can define a class that uses the Implements keyword to implement all the methods in an interface One classFishImplementsanimal{ A Public voidrun () { -System.out.println ("Fish is swimming"); - } the - Public voidBreathe () { -System.out.println ("Fish is bubbling"); - } + } - + //You can define an abstract class and use the Implements keyword to implement some of the methods defined in an Access port. A Abstract classLandanimalImplementsanimal{ at Public voidBreathe () { -System.out.println ("Landanimal is Breathing"); - } -}
In Java, the purpose of designing an interface is to allow classes to not be constrained by a single inheritance relationship, but to have the flexibility to inherit some common features at the same time, thus achieving multiple inheritance, and avoiding the problems arising from the complex relationships of multiple inheritance in C + +. The danger of multiple inheritance is that a class may inherit different implementations of the same method, which is not the case for interfaces, because the interface does not have any implementations.
A class can implement one or more interfaces while inheriting one parent class, and the extends keyword must precede the Implements keyword. As follows:
1 classStudentextendsPersonImplementsrunner{2 PublicStudent (intAge ) {3 Super(age);4 }5 6 Public voidrun () {7System.out.println ("The student is running");8 }9}
An example of a class implementing multiple interfaces:
1 class Bird implements runner,flyer{ 2 Span style= "color: #0000ff;" >public run () { 3 System.out.println ("The Bird ID running" 4 5 public void Fly () { 6 System.out.println ("The Bird is Flying" ); 7 8 }
The following is an example of a constant defined in an interface:
1 Public classTestDebug11 {2 Public Static voidMain (string[] args) {3Fish fish =NewFish ();4 intj = 0;5j = runner.id;//format: class. member Variables6j = fish.id;//object name. Static member7 }8}
3. Summary:
A. Implementing an interface is all the methods to implement the interface (except abstract classes);
B. The methods in the interface are abstract methods;
C. Multiple unrelated classes can implement the same interface, and a class can implement multiple unrelated interfaces.
Java abstract classes and interfaces