Interface
Can be understood as a special class, all of which are composed of global constants (static final) and public abstract methods.
The definition format of the interface
The data member of the interface, which is allowed only by public, static, final decoration. The method members of the interface, which are only allowed to be public, abstract decorated.
Interface Interface Name {
private static final int var1=1; Error
public static final int var2 = 2;
public abstract void fun1 ();
Private abstract int fun2 (); Error.
}
The data members in the interface are static final types, must be initialized, and the values of the data members of the interface cannot be modified, allowing the omission of the static, final keyword. The method in the interface must be an "abstract method", cannot have a method body, allow omitting public and abstract keywords
public interface a{
int num; Error. Not initialized
String name = "Test";
public void GetName () {...} Error. Not allowed to define method body
}
Interface implementation interfaces cannot directly produce objects with the new operator, must use their attributes to design new classes, and then use new classes to create objects like abstract classes, the interface must also be used by subclasses, subclasses through the Implements keyword implementation interface. Implementation format: Class subclass name implements interface A, interface b,....{///subclass member Declaration} The use of interfaces must pass through subclasses, and subclasses must overwrite all abstract methods. A subclass can only inherit from one parent class, but it is possible to implement multiple interfaces at the same time.
Interface a{
Public String name = "Xiaoming";
public void print ();
Public String getName ();
}
Interface b{
public void SetName ();
}
Class C implements A, b{
public void print () {...};
Public String GetName () {...};
public void SetName () {...};
}
All methods in the interface must be implemented, otherwise the class implementing the interface will have an abstract method and therefore become an abstract class. When you overwrite a method from an interface, you must declare it as public.
Class C implements A, b{
public void print () {...}; Did not overwrite the print method, but compiled.
Public String GetName () {...};
public void SetName () {...};
}
Object transformation
An object can be transformed into an interface type implemented by its owning class. Interfaces cannot be instantiated through new, but they can declare interface variables.
public class Test implements a{
Intidx = 1;
public void print () {
System.out.println ("")
}
Public String GetName () {}
public static void Main (String []agrs) {
Test test = new test ();
A = (a) test; Object transformation to interface type
System.out.println (A.name); The value of the output name
A.idx = 2; Error. IDX belongs to test and does not exist in interface a
A.print (); Executes the print method of test.
}
}
Inheriting an abstract class implementation interface
A subclass can inherit the abstract class and implement the interface format as follows: Class subclass extends abstract class implements interface A, interface B,...... { }
Inheritance of interfaces
An interface cannot inherit an abstract class, but can inherit from multiple interfaces simultaneously through extends.
Public abstract class Base {...}
Interface B {...}
Interface c{...} i
Nterface A extends base{...} Error
Interface A extends B, c{...} That's right
The difference between an interface and an inheritance
No.
Difference
Abstract class
Interface
1. Definition:
Abstract declares that the abstraction method is dispensable.
A interface declaration, composed of static constants and abstract methods.
2. Composition:
Construction methods, abstract methods, common methods, constants, variables.
Static constants, abstract methods.
3. Use:
Subclasses inherit abstract classes (extends).
The subclass implements the interface (implements).
4. Relationship:
Abstract classes can implement multiple interfaces.
An interface cannot inherit an abstract class, but it allows multiple interfaces to be inherited.
5. Object:
All produce instanced objects through the polymorphism of the objects.
6. Limitations:
Abstract classes have the limitations of single inheritance.
Interfaces can implement multiple inheritance.
7. Select:
If both abstract classes and interfaces are available, it is preferable to use interfaces to avoid the limitations of single inheritance.
Polymorphic
means that different types of objects can respond to the same message, that multiple types derived from the same base class can be treated as the same type, and that the same processing can be done for these different types, and because of polymorphism, these different derived class objects behave differently when they respond to the same method.
For example
All objects of the object class respond to the ToString () method
There are two main types of polymorphism in Java:
The overloaded and overridden methods.
The polymorphism of the object.
The polymorphism of the object
The polymorphism of the object is mainly divided into the following two types:
1. Upward transformation: Subclass object, Parent class object
For an upward transformation, the program is automatically completed.
BaseClass is a parent class, DerivedClass is a derived class of BaseClass
BaseClass BC = new DerivedClass (); An implicit transformation.
2. Transition down: Child class object, parent class object
For a downward transition, you must explicitly indicate the type of subclass you want to transform.
BaseClass BC = new DerivedClass (); First Upward transformation
DerivedClass DC = (derivedclass) BC;
public class a{
String name = "A";
public void Fun1 () {
System.out.println ("a->fun1");
}
public void fun2 () {
System.out.println ("a->fun2");
}
}
public class B extends a{
String name = "B";
public void Fun1 () {
System.out.println ("b->fun1");
}
public void Fun3 () {
System.out.println ("B->fun3");
}
}
Upward transformation:
public class demo{
public static void Main (String args[]) {
A = new B ();
A.fun1 (); Output what? B->fun1
A.fun2 (); Output what? A->fun2
A.fun3 (); Error. Fun3 method not defined in a
System.out.println (A.name); Output what?
}
}
Down transformation
public class demo{
public static void Main (String args[]) {
b b = (b) new A (); Forced transformation, throwing exceptions after running
A = new B ();
b b = (b) A; Down transformation
B.fun1 (); Output what? A->fun1
B.fun2 (); Output what? A->fun2
B.fun3 (); Output what? B->fun3
}
}
To create a downward transformation of an object, you must first generate an upward transformation relationship, a = new B (); Represents the establishment of a relationship.
The purpose of polymorphism
All objects can be transformed into the same type, and responding to the same message makes the code simple and easy to understand, making the program "extensible".
Application examples
Technology Foundation? Transformation: A reference variable for a parent class can point to a different subclass object. Dynamic binding: The runtime executes the appropriate subclass method based on the actual type of the object that the parent refers to the variable, thereby enabling polymorphism. Example 1: Driver driving public abstract class driver{
Public Driver () {}
public abstract void drives ();
}
public class Femaledriver extends Driver {
Public Femaledriver () {}
public void drives () {
System.out.println ("A Female driver drives a vehicle.");
}
}
public class Maledriver extends Driver {
Public Maledriver () {}
public void drives () {
System.out.println ("A Male driver drives a vehicle.");
}
}
public class test{
public static void Main (String [] args) {
Driver a = new Femaledriver ();
Driver B = new Maledriver ();
A.drives ();
B.drives ();
}
}
Output result: A Female driver drives a vehicle.
A Male driver drives a vehicle.
Example 2--the driver does not take advantage of the practice of object polymorphism
public class Maledriver extends Driver {
Public Maledriver () {}
public void Drivesbus () {
System.out.println ("A Male driver drives a bus.");
}
public void Drivescar () {
System.out.println ("A Male driver drives a car.");
}
public void Drivestruck () {
System.out.println ("A Male driver drives a truck.");
}
......
}
Using the approach of object polymorphism
Public abstract class vehicle{
public void Drivedbyfemale ();
public void Drivedbymale ();
}
public class Bus extends vehicle{
public void Drivedbyfemale () {
System.out.println ("A Female driver drives a bus.");
}
public void Drivedbymale () {
System.out.println ("A Male driver drives a bus.");
}
}
public class Maledriver extends Driver {
public void drives (Vehicle v) {
V. Drivedbymale ();
}
}
public class test{
public static void Main () {
Vehicle bus = new bus ();
Driver male = new Maledriver ();
Male.drives (bus);
}
}
Output result: A Male driver drives a bus
Java Interfaces and polymorphic