Abstract Class In Java

Java is one of the most widely used programming languages in the world and hence there are many aspirants for a career in java programming. In order to cater to the ever increasing demand, many java training tutorials have come up in the recent past. Abstract class in Java is one of the most important portions in java tutorials which will be dealt in detail during the training.

Before going into the details of abstract class in java you should know, what is an abstraction in java? The real meaning of the word “Abstraction” is “the quality of dealing with ideas rather than events”. In software terminology, the word abstraction has almost the same meaning. Abstraction in object oriented programming is the process to hide the implementation details from the user and provide only the functionality. For example, when you want to send an email, all you will have to do is to give an address and click on the send button. The protocol the email server uses is hidden from the user. Abstraction is achieved using abstract classes and interfaces in java.

What is Abstract Class in Java
An Abstract class in Java is a class that contains abstract as a keyword in its declaration. Although it is not necessary that abstract class contains abstract methods or methods without a body, if a class contains at least one abstract method, the class is declared as an abstract class.

Example for abstract class.
This is an excellent example for abstract class in java. You should use abstract keyword before the class keyword for creating an abstract class.

/* 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;
}
}

What is Abstract Method
When you insist that a class should contain a specific method but the actual implementation of the method should be determined by the child class, the method can be declared in the parent class as an abstract using abstract keyword. In this case, you should keep the abstract keyword before the name of the method in the declaration. Abstract method will not have a body but only a signature. Here is an example for abstract method.

public abstract class Employee {
private String name;
private String address;
private int number;

public abstract double computePay();
// Remainder of class definition
}

Leave a Reply

Your email address will not be published. Required fields are marked *