This is the 12th day of my participation in the August Text Challenge.More challenges in August
Java classes
In object-oriented concepts, all objects are represented by classes, but conversely, not all classes are represented by objects. If a class does not contain enough information to represent a concrete object, such a class is an abstract class.
In addition to not being able to instantiate objects, the rest of the functionality of an abstract class still exists. Member variables, member methods, and constructors are accessed just like normal classes.
Because an abstract class cannot instantiate an object, it must be inherited before it can be used. For this reason, the decision to design an abstract class is often made during the design phase.
The parent class contains the common methods of the subclass collection, but because the parent class itself is abstract, these methods cannot be used.
Abstract classes represent an inheritance relationship in Java. A class can only inherit one abstract class, but a class can implement multiple interfaces.
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 that method as abstract in the parent class.
The Abstract keyword can also be used to declare Abstract methods, which contain only a method name and no method body.
Abstract methods are undefined, and the method name is followed by a semicolon instead of curly braces.
Declaring abstract methods results in two things:
- If a class contains abstract methods, the class must be abstract.
- Any subclass must override its parent’s abstract methods or declare itself an abstract class.
Subclasses that inherit an abstract method must override that method. Otherwise, the subclass must also be declared abstract. Finally, a subclass must implement the abstract method; otherwise, neither the original parent class nor the final subclass can be used to instantiate the object.
/** * obj_3 * abstract class */
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 void mailCheck(a)
{
System.out.println("Mailing a check to " + this.name
+ "" + this.address);
}
public String toString(a)
{
return name + "" + address + "" + number;
}
public String getName(a)
{
return name;
}
public String getAddress(a)
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber(a)
{
return number;
}
/** * abstract method *@return* /
public abstract double computePay(a);
}
/** * obj_3 */
public class Salary extends Employee {
private double salary;
public Salary(String name, String address, int number,double salary){
super(name, address, number);
setSalary(salary);
}
public void mailCheck(a)
{
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + this.salary);
}
public double getSalary(a)
{
return this.salary;
}
public void setSalary(double newSalary)
{
if(newSalary >= 0.0)
{
this.salary = newSalary; }}public double computePay(a)
{
System.out.println("Computing salary pay for " + getName());
return this.salary/52; }}public class Obj_3 {
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(); }}Copy the code
Abstract class summary specification
-
- Abstract classes cannot be instantiated (a common mistake for beginners), and if instantiated, an error will be reported and the compilation will fail. Only non-abstract subclasses of an abstract class can create objects.
-
- An abstract class does not necessarily contain abstract methods, but a class with abstract methods must be an abstract class.
-
- Abstract method in abstract class is only declared, does not contain the method body, is not given the concrete implementation of the method is the concrete function of the method.
-
- Class methods (static methods) cannot be declared as abstract methods.
-
- A subclass of an abstract class must give a concrete implementation of an abstract method in the abstract class, unless the subclass is also an abstract class.