Original address: http://www.work100.net/training/java-abstract.html
More Tutorials: Beam Cloud - Free Course
abstract class
Sequence Number | Intratext chapters | video |
---|---|---|
1 | Summary | - |
2 | Java abstract class | - |
3 | Inherit abstract class | - |
4 | Abstract method | - |
5 | summary | - |
See the navigation section above for reading
1. Overview
In the object-oriented concept, all objects are portrayed by classes, but conversely, not all classes are used to portray objects. If a class does not contain enough information to portray a specific object, such a class is an abstract class.
In addition to the fact that abstract classes cannot instantiate objects, other functions of the class still exist, and member variables, member methods, and construction methods are accessed in the same way as regular classes.
Since abstract classes cannot instantiate objects, they must be inherited in order to be used.For this reason, it is common to decide at the design stage whether or not to design Abstract classes.
Parents contain common methods for collections of subclasses, but they cannot be used because the parent itself is abstract.
In Java, an abstract class represents an inheritance relationship. A class can inherit only one abstract class, while a class can implement multiple interfaces.
2.Java abstract classes
Use abstract classes to define abstract classes in the Java language.Examples include the following:
/* Filename: Employee.java */ public abstract class Employee { private String name; private String address; private int number; public Employee(String name, String address, int number) { System.out.println("Constructing an Employee"); this.name = name; this.address = address; this.number = number; } public double computePay() { System.out.println("Inside Employee computePay"); return 0.0; } public void mailCheck() { System.out.println("Mailing a check to " + this.name + " " + this.address); } public String toString() { return name + " " + address + " " + number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address = newAddress; } public int getNumber() { return number; } }
Notice that the Employee class is no different, although it is an abstract class, but it still has three member variables, seven member methods, and one construction method.Now if you try the following example:
/* Filename: AbstractDemo.java */ public class AbstractDemo { public static void main(String [] args) { /* The following are not allowed and will cause errors */ Employee e = new Employee("George W.", "Houston, TX", 43); System.out.println("\n Call mailCheck using Employee reference--"); e.mailCheck(); } }
When you try to compile the AbstractDemo class, the following errors occur:
Employee.java:46: Employee is abstract; cannot be instantiated Employee e = new Employee("George W.", "Houston, TX", 43); ^ 1 error
3. Inherit abstract classes
We can inherit the Employee class in a general way:
/* Filename: Salary.java */ public class Salary extends Employee { private double salary; //Annual salary public Salary(String name, String address, int number, double salary) { super(name, address, number); setSalary(salary); } public void mailCheck() { System.out.println("Within mailCheck of Salary class "); System.out.println("Mailing check to " + getName() + " with salary " + salary); } public double getSalary() { return salary; } public void setSalary(double newSalary) { if(newSalary >= 0.0) { salary = newSalary; } } public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } }
Although we cannot instantiate an object of the Employee class, if we instantiate a Salary class object, the object inherits seven member methods from the Employee class, which allow us to set or get three member variables.
/* Filename: AbstractDemo.java */ public class AbstractDemo { public static void main(String [] args) { Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00); Employee e = 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 above program compiles and runs as follows:
Constructing an Employee Constructing an Employee Call mailCheck using Salary reference -- Within mailCheck of Salary class Mailing check to Mohd Mohtashim with salary 3600.0 Call mailCheck using Employee reference-- Within mailCheck of Salary class Mailing check to John Adams with salary 2400.
4. Abstract methods
If you want to design a class that contains a particular member method whose implementation is determined by its subclasses, you can declare the method Abstract in the parent class.
The Abstract keyword can also be used to declare Abstract methods, which contain only one method name and no method body.
The abstract method is undefined, and the method name is followed directly by a semicolon, not curly brackets.
public abstract class Employee { private String name; private String address; private int number; public abstract double computePay(); //Remaining code }
Declaring an abstract method results in the following two results:
- If a class contains an abstract method, it must be an abstract class
- Any subclass must override the abstract method of the parent class or declare itself Abstract
Subclasses that inherit abstract methods must override the method.Otherwise, the subclass must also be declared abstract.Ultimately, the abstract method must be implemented by subclasses, otherwise, objects cannot be instantiated from the original parent to the final subclass.
If the Salary class inherits the Employee class, it must implement the computePay() method:
/* Filename: Salary.java */ public class Salary extends Employee { private double salary; // Annual salary public double computePay() { System.out.println("Computing salary pay for " + getName()); return salary/52; } //Remaining code }
5. Summary
- Abstract classes cannot be instantiated (errors that beginners can easily make), and if instantiated, errors will occur and compilation will fail.Only non-abstract subclasses of abstract classes can create objects.
- An abstract class does not necessarily contain an abstract method, but a class that has an abstract method must be an abstract class.
- An abstract method in an abstract class is only a declaration, does not contain a method body, or does not give a concrete implementation of the method, that is, the specific function of the method.
- Construction methods, class methods (methods decorated with static) cannot be declared abstract.
- A subclass of an abstract class must give a concrete implementation of the abstract method in the abstract class unless it is also an abstract class.
Last: polymorphic
Next: encapsulation