Abstract class:
Scene:
Multiple classes inherit a class, and when we need to call a method, because the method body is different, it has an object feature. This time the base class creates an abstract method that takes the place of a subclass that implements a concrete abstract method.
If there is an abstract method in a class, then this class is also an abstract class, which needs to be modified by abstract.
Abstract classes are not instantiated and need to invoke their subclasses to call the base class's abstract methods and common methods and data.
If the subclass does not implement all of the abstract methods, then the subclass is also an abstract class.
By instantiating subclasses, the corresponding abstract methods of various subclasses are called by the parent class to achieve polymorphic purposes.
1 Package Com.company;2 3 Public classInit_java {4 Public Static voidMain (string[] args) {5 person[] per_l=new person[2];6 per_l[0]=new Employee ("Tom", "evil");7 per_l[1]=new Student ("Tom", "Tom");8 for(personp:per_l) {9System. out. println (P.kouhao ());Ten } One //System.out.println (A.kouhao ()); A //System.out.println (B.kouhao ()); - - } the - - } - + Abstract classperson{ - PrivateString name; + PublicPerson (String aname) { A This. name=Aname; at } - PublicString GetName () { - return This. Name; - } - Public AbstractString Kouhao (); - } in - classEmployee extends person{ to PrivateString Alaname; + PublicEmployee (String name1,string name2) { - super (NAME1); the This. alaname=name2; * } $ PublicString Kouhao () {Panax Notoginseng return "Employee"+ This. alaname+"is great good!"; - }; the PublicString Getalaname () { + return This. Alaname; A } the + } - classStudent extends person{ $ PrivateString name; $ PublicStudent (String name1,string name2) { - super (NAME1); - This. name=name2; the } - PublicString Kouhao () {Wuyi return "Student"+ This. name+"is beautiful!"; the } -}
Output Result:
By the definition of the initial abstract object, the subclass implements an abstract method, which initializes the parent class by subclasses at the time of invocation, and uses the parent class variable to invoke the same method of the subclass (Kouhao).
Java abstract class