interfaces, inheritance, and polymorphism
1. Inheritance and polymorphism is an important part of the object-oriented development language, which makes the architecture of the whole program very flexible and reduces code redundancy.
2. Inheritance: reuse of defined classes;
3. Polymorphism: Can dynamically adjust the call of the object, reduce the dependency relationship;
4. Interface: Classes in Java can implement multiple interfaces, which are used to establish standards between classes and classes;
Interface * * *
1. Purpose of the interface:
The Java language supports only single inheritance, that is, a class can have only one parent class.
In practical application, multiple inheritance is often required to solve the problem, and the multiple inheritance of the interface implementation class is provided.
2. Interface definition: Similar to the definition of a class, the Declaration and interface body of an interface;
[Modifier] Interface Interface Name [extends parent interface List]{
[Public] [Static] [Final] variable;
[Public] [Abstract] method;
}
3. The method in the interface is not a method body;
4. Implementation of the interface:
After the interface is defined, the interface can be implemented in the class, using implements;
[Modifier] Class < class name > [Extends parent class name][implements interface list]{}
4. In the inheritance of the class can only do single-inheritance, and implementation of the interface, one can implement multiple interfaces, separated by commas;
This may occur when a variable name or method conflict occurs, an interface that explicitly specifies the variable, "interface name. Variable" Implementation;
1 Interfacedrawtest {//Defining Interfaces2 Public voidDraw ();//Defining Methods3 }4 5 //defines the parallelogram class, which inherits the Quadrilateral class and implements the Drawtest interface6 classParallelogramgleuseinterfaceextendsQuadrangleuseinterface7 ImplementsDrawtest {8 Public voidDraw () {//because the class implements the interface, the draw () method needs to be overridden9System.out.println ("parallelogram. Draw ()");Ten } One A voidDoanything () {//overriding the parent class method - //somesentence - } the } - - classSquareuseinterfaceextendsQuadrangleuseinterfaceImplements - Drawtest { + Public voidDraw () { -System.out.println ("square. Draw ()"); + } A at voiddoanything () { - //somesentence - } - } - - classAnythinguseinterfaceextendsQuadrangleuseinterface { in voiddoanything () { - to } + } - the Public classQuadrangleuseinterface {//Defining quadrilateral Classes * Public voiddoanytthing () { $ //somesentencePanax Notoginseng } - the Public Static voidMain (string[] args) { +Drawtest[] D = {//interface can also be used for up-conversion operations A NewSquareuseinterface (),Newparallelogramgleuseinterface ()}; the for(inti = 0; i < d.length; i++) { +D[i].draw ();//call the Draw () method - } $ } $}
Output Result:
1 interfaces, inheritance, and polymorphism 2 3 To find the area and perimeter of circle by using interface 4 The circumference of the circle is: 31.400002 5 0 of the area is: 78.5 6 7 interface implementation picture Save in different formats 8 Save the picture in GIF format ~ 9 define Gpscar class, inherit car and implement interface GPS one- java.awt.point[x=888,y=888] car name: Benz speed: 888-kilometer/ H coordinates: 888,888
No2_1. Interface Inheritance Polymorphism _java Learning Notes _ interface