Concept:
Abstract class: If a class does not contain enough information to depict a specific object, such a class is an abstract class.
Abstract method: A method in an abstract class that has no specific effect.
Characteristics:
Abstract class: Except that objects cannot be instantiated, other functions of the class persist, and member variables, member methods, and constructor methods are accessed in the same way as normal classes. Abstract classes must be inherited to be used, and a class can inherit only one abstract class. Abstract classes cannot be used directly with new.
Abstract method: no function body.
Abstract classes can have no abstract methods, and classes with abstract methods must be abstract classes. In a derived class, you can not implement some abstract methods. In abstract classes, there can be abstract methods or non-abstract methods.
Abstract class
Abstract classes are used in the Java language to define abstractions. The following example:
Public Abstract classEmployee//Abstract class name{ PrivateString name; PrivateString address; Private intNumber ; PublicEmployee (string name, address string,intNumber ) {System.out.println ("Constructing an Employee"); This. Name =name; This. Address =address; This. Number =Number ; } Public DoubleComputepay () {System.out.println ("Inside Employee Computepay"); return0.0; } Public voidMailCheck () {System.out.println ("Mailing a check to" + This. Name+ " " + This. Address); } PublicString toString () {returnName + "+ address +" "+Number ; } PublicString GetName () {returnname; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String newaddress) {address=newaddress; } Public intGetNumber () {returnNumber ; }}
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:
Public class abstractdemo{ publicstaticvoid main (String [] args) { /* */ new Employee ("George W.", "Houston, TX", "Max"); System.out.println ("\ n call MailCheck using Employee reference--"); E.mailcheck (); }}
When you try to compile the Abstractdemo class, an error occurs.
Inheriting abstract classes
We can inherit the employee class in a general way:
Public classSalaryextendsEmployee//Inherit abstract class employee (employee){ Private DoubleSalary//Annual salary (annual salary) PublicSalary (string name, string address,intNumberDoublesalary) { Super(name, address, number); Setsalary (Salary); } Public voidMailCheck () {System.out.println ("Within MailCheck of Salary class"); System.out.println ("Mailing Check to" +GetName ()+ "with salary" +salary); } Public Doublegetsalary () {returnsalary; } Public voidSetsalary (Doublenewsalary) { if(Newsalary >= 0.0) {Salary=newsalary; } } Public DoubleComputepay () {System.out.println ("Computing salary pay for" +getName ()); returnSalary/52; }}
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.
Public class abstractdemo{ publicstaticvoid main (String [] args) { New Salary ("Mohd Mohtashim", "Ambehta, Up", 3, 3600.00); New Salary ("John Adams", "Boston, MA", 2, 2400.00); System.out.println ("call MailCheck using Salary reference--"); S.mailcheck (); System.out.println ("\ n call MailCheck using Employee reference--"); E.mailcheck (); }}
The results of the above program compilation Run as follows:
constructing an employeeconstructing an employeecall mailcheck using --Class 3600.0 callMailCheck using Employee reference-class 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.
Public Abstract class employee{ private String name; Private String address; Private int Number ; Public Abstract double Computepay (); // abstract Methods // remaining code }
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:
Public class extends employee{ privatedouble// Annual salary Public double Computepay ()// override Computepay method { System.out.println ( "Computing salary pay for" + GetName ()); return salary/52; } // remaining code }
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