Document directory
- 1.1 abstract class implementation Interface
- 2.1 Interface Definition
1. abstract classes and common classes
- Abstract Classes define an abstract method more than common classes. There is no difference except that objects cannot be instantiated directly.
- An abstract class cannot be declared using the final keyword, because the class declared by final cannot be inherited, and the abstract class must be rewritten for the rest of its life.
- An abstract class can define the constructor.
- Do not use private Declaration for Abstract METHODS: Private modification is not allowed when abstract keywords are used, because abstract methods must be overwritten. If private declaration is used, the subclass cannot be overwritten.
- Java allows an abstract class to implement multiple interfaces, and the abstract class to implement interfaces does not need to implement methods defined in interfaces. Example:
1.1 abstract class implementation Interface
Define interface interface1
View code
public interface interface1 { public int print2();}
Define the abstract class abstractclass1 implementation interface interface1
View code
public abstract class abstractclass1 implements interface1 {}
2. Interface
- The interface can be understood as a special classGlobal constantAndCommon Abstract METHODS.
- Abstract METHODS In interfaces must be definedPublicAccess permission, which cannot be changed. Even if the interface does not write public information, it is also a public access permission.
- In many Java programs, public is often omitted when writing programming interface methods, so many readers think that the access permission is default, which is actually incorrect, no matter whether it is written or not, the methods in the interface are always public.
- Interfaces can inherit (extends) interfaces, and canImplement Multi-inheritance of interfacesFor example, public interface interface3ExtendsInterface1, interface2 {}. However, there is a requirement that there is no conflicting method definition in interface1 and interface2 interfaces. Conflicting methods are defined as methods with the same method name but different return types or parameters.
- The interface cannot inherit abstract classes.
- Java only supports single inheritance for classes, but a class can implement multiple interfaces, which is equivalent to avoiding the limitations of single inheritance. If multiple interfaces are implemented with the same method (the parameters and return types are the same), the Implementation interface is equivalent to the method that implements both interfaces at the same time. If the Implemented interfaces have methods of the same name but different types, these methods must be rewritten in the class.
2.1 interface definition view code
Public interface a {public static final string name = "zhangsan"; public abstract void print (); public abstract string getinfo ();}
In the basic concepts of interfaces, it is clearly stated that interfaces are composed of global constants and public abstract methods. Therefore, the preceding interfaces can be abbreviated as follows:
View code
Public interface a {string name = "zhangsan"; void print (); string getinfo ();}
3. Object Polymorphism
There are two types of polymorphism in Java:
- Override and overload)
- Object Polymorphism
There are two types of object polymorphism:
- Upward transition: subclass Object> parent class object. Instantiate the parent class with a subclass. Parent class Object = subclass instance. Automatic Program completion
- Downward transition: parent class Object> subclass object. Instantiate a subclass with the parent class. Subclass subclass object = (subclass) parent class instance. You must specify the subclass type to be transformed. Here (subclass) is forced type conversion.Before the object's downward transformation, the object's upward transformation must first take place.
Multi-state instances
Parent Class
View code
public class A { public void fun1() { System.out.println("A-->public void fun1(){}"); } public void fun2() { this.fun1(); }}
Subclass B
View code
public class B extends A { public void fun1() { System.out.println("B-->public void fun1(){}"); } public void fun3() { System.out.println("B-->public void fun3(){}"); }}
Main Program polydemo2
View code
Public class polydemo2 {public static void main (string [] ARGs) {A = new B (); // transforms. fun1 (); // If an upward transformation occurs, call the method. fun2 (); // fun2 is not overwritten by the quilt class. It calls the method of parent class A, but fun2 calls its own fun1, // and fun1 is overwritten by quilt Class B, therefore, call fun1 B = (B) A in subclass B; // perform downward transformation B. fun1 (); // call its own method B. fun2 (); // call fun2 of parent class A, but in parent class A fun2 calls fun1, // fun1 is overwritten by subcategory B, so it still calls fun1 B of subclass B. fun3 (); // call the defined fun3 .}}
Program output result:
B --> Public void fun1 (){}
B --> Public void fun1 (){}
B --> Public void fun1 (){}
B --> Public void fun1 (){}
B --> Public void fun3 (){}