I. Abstract class 1, syntax definition
Classes that use the abstract adornment in front of a class are abstract classes
2. Application Scenario:
(1) In some cases, a parent class simply knows how its subclasses should be, but does not know exactly how these subclasses implement these methods.
(2) An abstract class is abstracted from multiple subclasses with the same characteristics, and the abstract class is used as the template of the subclass to avoid the arbitrariness of the sub-class design.
3. Function:
Restricts certain methods that a subclass must implement, but does not focus on implementation details.
4. Rules of Use:
(1) Defining abstract classes using abstract
(2) Abstract definition abstraction method, only declaration, do not need to implement
(3) Abstract classes can contain generic methods, not necessarily abstract methods, but classes containing abstract methods must be declared as abstract classes
(4) It is superfluous to define a construction method in an abstract class
(5) Abstract classes cannot be created directly, you can define reference variables
(6) Abstract cannot be modified in the same way as private, static, final, or native
(7) If a class is defined as an abstract class, the class cannot create a concrete instance object with the new operator, and the method in the abstract class must be implemented in an overridden manner
For geometric shapes, which are abstract concepts that derive specific geometries such as "rectangle" and "circle", the shape is defined as an abstract parent class. Also, if you want to specify that each shape class write a method that calculates its area and length. Because each specific geometry has a different area and perimeter calculation method, it is inappropriate to place the parent class. It can be defined as an abstract method, which is defined in a subclass
public class Demo1 {/** * @param args */public static void main (string[] args) {Shape rec = new Rectangle ("Rectangle", 3.5, 5); Declares the parent class object, pointing to the Subclass object System.out.print ("area =" +rec.getarea ()); System.out.println ("perimeter =" +rec.getlength ()); Circle Cir = new Circle ("Circle", 3.5); System.out.print ("area =" +cir.getarea ()); System.out.print ("perimeter =" +cir.getlength ());}} Abstract class shape{//define Abstraction classes shapeprotected string Name;public Shape (string xm) {name = XM; System.out.println ("Name:" +name);} Abstract public double Getarea (); Declares an abstract public method, abstract public, double GetLength ();} Class Circle extends shape//definition inherits subclasses from Shape Circle{private final double PI = 3.14;private double radius;public Circle (stri ng shapename,double R)//construction method {super (shapename); radius=r;} Public double Getarea () {return pi*radius*radius;} Public double GetLength () {return 2*pi*radius;}} Class Rectangle extends shape//definition inherits subclasses from shape Rectangle{private double width;private double height;public Rectangle (Str ing shapename,double width,double height) {Super (Shapename); this.width = Width;this.height = height;} Public double Getarea () {return width*height;} Public double GetLength () {return (width+height);}}
Second, interface 1, interface definition
The interface defines the specifications that a batch of classes need to follow, the interfaces do not care about the internal data of these classes, and do not care about the specific implementations of these class methods, which specify only certain methods that must be provided in these classes.
An interface is actually a special kind of abstract class.
2, the definition method:
Use the interface keyword:
[Modifier] interface Interface Name [extends Parent Interface 1, parent Interface 2 ...] { 0 ~ Multiple Constants define 0 ~ Multiple abstract Method definitions}
3. Interface Definition:
The method in the interface can only be an abstract method and is always used, even if the public abstract modifier is not added to the definition, and the system is added by default.
4, using the interface:
(1) The data member of the interface must be initialized
(2) The methods in the interface must all be declared abstract, and the methods of the interfaces must all be abstracted methods
Format fixed, can not change extends and implements order of exchange.
Build Interface:
You can write it yourself.
1 Public classDemo1 {2 /**3 * @paramargs4 */5 Public Static voidMain (string[] args) {6IShape Cir =NewCircle (3.5);7System.out.print ("area =" +Cir.getarea ());8System.out.println ("perimeter =" +cir.getlength ());9Rectangle rec =NewRectangle (3.5, 5);//declares the parent class object, pointing to the subclass objectTenSystem.out.print ("area =" +Rec.getarea ()); OneSystem.out.println ("perimeter =" +rec.getlength ()); A - } - } the - InterfaceIShape { - Final DoublePI = 3.14; - Abstract DoubleGetarea ();//declaring abstract methods + Abstract Doublegetlength (); - + } A at - - classCircleImplementsIShape//using the IShape interface to implement the Circle class - { - Private Doubleradius; - PublicCircle (DoubleR//Construction Method in { - This. radius=R; to } + Public DoubleGetarea () { - returnpi*radius*radius; the } * Public DoubleGetLength () { $ return2*pi*radius;Panax Notoginseng } - the } + A classRectangleImplementsIShape//using the IShape interface to implement the Circle class the { + Private Doublewidth; - Private Doubleheight; $ PublicRectangle (DoubleWidthDoubleheight) $ { - This. width =width; - This. Height =height; the } - Public DoubleGetarea () {Wuyi the returnwidth*height; - } Wu Public DoubleGetLength () { - About return(width+height); $ } -}View Code5, the inheritance of the interface:
Similar to the class, the interface has inheritance, the interface can be defined through the Extend keyword declaration that the new interface is a child interface that already exists parent interface, will inherit all the variables and methods of the parent interface.
Unlike class inheritance, an interface can have more than one parent interface, separated by ",". Overrides are overridden if a constant with the same name as the parent interface or the same method is defined in the sub-interface.
Third, inner class and anonymous class
An inner class is a class that is defined in a class, and the primary function is to put logically related classes together, whereas an anonymous class is a special inner class, without a class name, and when the class is defined, an instance of that class is generated, so no name is used because the class is no longer available.
JAVA abstract classes, interfaces