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 class
Abstract classes are used in the Java language to define abstractions. The following example:
Employee.java File Code:
/
file name: 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;
}
}
notes 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:
Abstractdemo.java file code:
/ file name: Abstractdemo.java /
public class Abstractdemo
{
public static void Main (String [] args)
{
/ The following is not allowed and an error is raised /
Employee e = new Employee ("George W.", " Houston, TX ",");
System.out.println ("\ n call MailCheck using Employee reference--");
E.mailcheck ();
}
}
When you try to compile the Abstractdemo class, the following error occurs:
Employee.java:46:employee is abstract; cannot be instantiated
Employee E = new Employee ("George W.", "Houston, TX", 43);
^
1 Error
Inheriting abstract class
We can inherit the employee class in a general way:
Salary.java file code:
/ 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 an employee class, if we instantiate a salary class object, the object inherits 7 member methods from the employee class, and the method allows you to set or get three member variables.
Abstractdemo.java File Code:
/ file name: 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 results of the above program compilation Run as follows:
Constructing an Employee
Constructing an Employee
Call bjrongjinhuiyin.com 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 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 ();
The rest of the code
}
Declaring an abstract method can result in the following two results:
If a molten gold silver 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 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:
Salary.java File Code:
/ file name: 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;
}
The rest of the code
}
Abstract class Summary provisions
- 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.
- Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.
- 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.
- A constructor method, a class method (a method that is decorated with static), cannot be declared as an abstract method.
- 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