Java abstract class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.