Java abstract class

Source: Internet
Author: User

Abstract class often used to characterize the abstract concepts of analysis and design of problem areas , is a series of look different, but essentially the same abstract of detailed concepts .


In object-oriented concepts, all objects are depicted through classes, but in turn. Not all classes are used to depict objects, assuming that a class does not include enough information to depict a detailed object, which is an abstract class.


Assuming a hierarchy of inheritance from the bottom up of a class, the classes on the upper level are more generic and may even be more abstract.

From a certain point of view, ancestor classes are more generic, and people simply use it as a base class for deriving other classes. Instead of being the specific instance class that you want to use.

Consider, for example, an extension to the person class hierarchy.

An employee is a person, and a student is a person. The following joins the employee and class student into the class hierarchy, for example by:


Why spend your energy on such a high-level abstraction? Each person has some attributes, such as the name. Both students and employees have a name attribute. It is therefore possible to place the GetName method in a generic superclass at a higher level of inheritance.

Now, add a GetDescription method that returns a short descriptive narrative of a person.

Like what:

An employee with a salary of $50,000.00

A student majoring in computer

Implementing this method in the employee class and the student class is very easy. But what should be provided in the person class? Except for the name. The person is ignorant. Of course, you can have person.getdescription () return an empty string. However. Another better way. is to use the abstractkeyword, so there is absolutely no need to implement this approach.

Public abstract String getdescription ();//no implemention required
In order to improve the clarity of the program. A class that includes one or more abstract methods must itself be declared abstract.

Abstract class person{...    Public abstract String getdescription ();}
In addition to abstract methods, abstract classes can include detailed data and detailed methods, such as:

Abstract class person{    private String name;    Public person (String N) {        name = n;    }    Public abstract String getdescription ();    Public String GetName () {        return name;    }}
In addition A smoke-like class cannot be instantiated, but it can reference objects of non-abstract subclasses.

Now, we define a class, which puts an abstract method, for other classes to inherit this abstract class, do not have to implement this abstract method, such as Getdescription,student and employee have described their own method getdescription, But the description of the narrative is certainly not the same.

Some of the interfaces to implement too many methods, and we only need one or two of them, then with the abstract class is a very good way:

public static abstract class Absmksearchlistener implements Mksearchlistener {@Overridepublic void Ongetaddrresult ( Mkaddrinfo arg0, int arg1) {//TODO auto-generated method stub} @Overridepublic void Ongetbusdetailresult (Mkbuslineresult arg0, int arg1) {//TODO auto-generated method stub} @Overridepublic void Ongetdrivingrouteresult (Mkdrivingrouteresult arg0, int arg1) {//TODO auto-generated method stub} @Overridepublic void Ongetpoidetailsearchresult (int arg0, int arg1) {/ /Todo auto-generated method stub} @Overridepublic void Ongetpoiresult (Mkpoiresult arg0, int arg1, int arg2) {//Todo auto- Generated method stub} @Overridepublic void Ongetshareurlresult (Mkshareurlresult arg0, int arg1, int arg2) {//TODO Auto-ge nerated method stub} @Overridepublic void Ongetsuggestionresult (Mksuggestionresult arg0, int arg1) {//TODO auto-generated method stub} @Overridepublic void Ongettransitrouteresult (Mktransitrouteresult arg0, int arg1) {//TODO auto-generated method stub} @Overridepublic void OngetwaLkingrouteresult (mkwalkingrouteresult arg0, int arg1) {//TODO auto-generated method stub}} 
We just want to ongetaddrresult this callback method, if the implementation of Mksearchlistener, we must implement all interfaces, so to write a lot more code and code redundancy, one or two to implement the interface. But too much. Using abstract classes is a good choice, just the methods inside are already empty .。 Therefore, when inheriting the abstract class Absmksearchlistener, it is possible to implement either method.


Java abstract class

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.