Java abstract class

Source: Internet
Author: User

Java Abstract class

In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class.

Abstract classes In addition to the object cannot be instantiated, other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class.

Because an abstract class cannot instantiate an object, an abstract class must be inherited in order to be used. For this reason, it is often decided at the design stage to not design abstract classes.

The parent class contains a common method for the collection of subclasses, but because the parent class itself is abstract, these methods cannot be used.

In Java, an abstract class represents an inheritance relationship in which a class can inherit only one abstract class, while a class may implement multiple interfaces.

Abstract classes are used in the Java language to define abstractions. The following example:

1  Public Abstract classEmployee2 {3    PrivateString name;4    PrivateString address;5    Private intNumber ;6     PublicEmployee (string name, address string,intNumber )7    {8SYSTEM.OUT.PRINTLN ("Constructing an Employee");9        This. Name =name;Ten        This. Address =address; One        This. Number =Number ; A    } -     Public DoubleComputepay () -    { theSystem.out.println ("Inside Employee Computepay"); -      return0.0; -    } -     Public voidMailCheck () +    { -SYSTEM.OUT.PRINTLN ("Mailing a check to" + This. Name ++ " " + This. address); A    } at     PublicString toString () -    { -       returnName + "+ address +" "+Number ; -    } -     PublicString getName () -    { in       returnname; -    } to     PublicString getaddress () +    { -       returnaddress; the    } *     Public voidsetaddress (String newaddress) $    {Panax NotoginsengAddress =newaddress; -    } the     Public intGetNumber () +    { A      returnNumber ; the    } +}

Notice that the employee class is not different, although the class is abstract, but it still has 3 member variables, 7 member methods, and a constructor method. Now if you try the following example:

1  Public classAbstractdemo2 {3     Public Static voidmain (String [] args)4    {5       /*The following are not allowed and will raise an error*/6Employee e =NewEmployee ("George W.", "Houston, TX", 43);7  8System.out.println ("\ n call MailCheck using Employee reference--");9 E.mailcheck ();Ten     } One}

When you try to compile the Abstractdemo class, the following error is generated:

1 Abstract ; cannot be instantiated 2       New Employee ("George W.", "Houston, TX"); 3                    ^4 1 Error
Inheriting abstract Classes

We can inherit the employee class in a general way:

1  Public classSalaryextendsEmployee2 {3    Private DoubleSalary//Annual Salary4     PublicSalary (string name, string address,intNumberDouble5 salary)6    {7        Super(name, address, number);8 setsalary (salary);9    }Ten     Public voidMailCheck () One    { ASystem.out.println ("Within MailCheck of Salary class"); -System.out.println ("Mailing Check to" +GetName () -+ "with salary" +salary); the    } -     Public Doublegetsalary () -    { -        returnsalary; +    } -     Public voidSetsalary (Doublenewsalary) +    { A        if(Newsalary >= 0.0) at        { -Salary =newsalary; -        } -    } -     Public DoubleComputepay () -    { inSystem.out.println ("Computing salary pay for" +getName ()); -       returnSalary/52; to    } +}

Although we cannot instantiate an object of an employee class, if we instantiate a salary class object, the object inherits 3 member variables and 7 member methods from the Employee class.

1  Public classAbstractdemo2 {3     Public Static voidmain (String [] args)4    {5Salary s =NewSalary ("Mohd Mohtashim", "Ambehta, Up", 3, 3600.00);6Employee e =NewSalary ("John Adams", "Boston, MA", 2, 2400.00);7  8System.out.println ("Call MailCheck using Salary reference--");9 S.mailcheck ();Ten   OneSystem.out.println ("\ n call MailCheck using Employee reference--"); A E.mailcheck (); -     } -}

The results of the above program compilation Run as follows:

1 Constructing an Employee 2 Constructing an Employee 3 call MailCheck using  Salary Reference--4class5 mailing Check to Mohd Mohtashim with salary 3600.067 call MailCheck using Employee reference--8class  9 mailing Check to John Adams with salary 2400.
Abstract Methods

If you want to design a class that contains a special member method whose implementation is determined by its subclasses, you can declare the method as an abstract method in the parent class.

The abstract keyword can also be used to declare an abstraction method, which contains only one method name and no method body.

Abstract methods are not defined, followed by a semicolon, not a curly brace, directly following the method name.

1  Public Abstract classEmployee2 {3    PrivateString name;4    PrivateString address;5    Private intNumber ;6    7     Public Abstract DoubleComputepay ();8    9    //The rest of the codeTen}

Declaring an abstract method can result in the following two results:

    • If a class contains an abstract method, the class must be an abstract class.
    • Any subclass must override the abstract method of the parent class, or declare itself an abstract class.

Subclasses of inherited abstract methods must override this method. Otherwise, the subclass must also be declared as an abstract class. Eventually, a subclass must implement the abstract method, otherwise, from the original parent class to the final subclass, it cannot be used to instantiate the object.

If the salary class inherits the employee class, it must implement the Computepay () method:

1  Public classSalaryextendsEmployee2 {3    Private DoubleSalary//Annual Salary4   5     Public DoubleComputepay ()6    {7System.out.println ("Computing salary pay for" +getName ());8       returnSalary/52;9    }Ten   One    //The rest of the code A}
Abstract class Summary provisions
    • 1. Abstract classes can not be instantiated (beginners are prone to make mistakes), if instantiated, will error, compile cannot pass. Only non-abstract subclasses of abstract classes can create objects.

    • 2. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.

    • 3. Abstract methods in abstract classes are only declarations, do not contain method bodies, that is, the concrete implementation of the method is the specific function of the method.

    • 4. A constructor method, a class method (a static decorated method) cannot be declared as an abstract method.

    • 5. Subclasses of an abstract class must give a concrete implementation of an abstract method in an abstract class, unless the subclass is also an abstract class.

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.