Java Abstract Classes
In object-oriented concepts, all objects are described through classes, but conversely, not all classes are used to describe objects. If a class does not contain enough information to describe a specific object, such a class is an abstract class.
Except that abstract classes cannot instantiate objects, other functions of the class still exist. Member variables, member methods, and constructors have the same access methods as ordinary classes.
Because abstract classes cannot instantiate objects, abstract classes must be inherited in order to be used. For this reason, it is usually decided during the design stage whether to design an abstract class.
The parent class contains common methods of the collection of subclasses, but because the parent class itself is abstract, these methods cannot be used.
In Java, abstract classes represent an inheritance relationship. A class can only inherit one abstract class, but a class can implement multiple interfaces.
Abstract Classes
In the Java language, use abstract class to define an abstract class. Example as follows:
Employee.java file code:
Note that this Employee class is no different. Although this class is an abstract class, it still has 3 member variables, 7 member methods, and 1 constructor. Now if you try the following example:
AbstractDemo.java file code:
When you try to compile the AbstractDemo class, the following error will be produced:
Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43);
^
1 error
Inheriting Abstract Classes
We can inherit the properties of the Employee class in the following way:
Salary.java file code:
Although we cannot instantiate an object of the Employee class, if we instantiate a Salary class object, that object will inherit 7 member methods from the Employee class, and through these methods the three member variables can be set or obtained.
AbstractDemo.java file code:
The above program compiles and runs with the following result:
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.
Abstract Methods
If you want to design such a class that contains a special member method whose specific implementation is determined by its subclasses, then you can declare this method as an abstract method in the parent class.
The Abstract keyword can also be used to declare abstract methods. An abstract method contains only a method name and has no method body.
An abstract method is not defined; the method name is directly followed by a semicolon instead of curly braces.
Declaring an abstract method results in the following two consequences:
- If a class contains an abstract method, then the class must be an abstract class.
- Any subclass must override the abstract method of the parent class, or declare itself as an abstract class.
A subclass that inherits an abstract method must override that method. Otherwise, the subclass must also be declared as an abstract class. Eventually, there must be a subclass that implements the abstract method; otherwise, from the original parent class to the final subclass, none of them can be used to instantiate objects.
If the Salary class inherits the Employee class, then it must implement the computePay() method:
Salary.java file code:
Abstract Class Summary Rules
1. Abstract classes cannot be instantiated (a mistake beginners often make). If instantiated, an error will be reported and compilation will fail. Only non-abstract subclasses of an abstract class can create objects.
2. An abstract class does not necessarily contain abstract methods, but a class with abstract methods must be an abstract class.
3. Abstract methods in an abstract class are only declarations and do not contain a method body, meaning the specific implementation of the method, that is, the specific functionality of the method, is not provided.
4. Constructors and class methods (methods modified with static) cannot be declared as abstract methods.
5. A subclass of an abstract class must provide specific implementations of the abstract methods in the abstract class, unless the subclass is also an abstract class.