logo

Java Abstraction


Show

As per vocabulary, abstraction is the superiority of commerce with thoughts rather than occurrences. For instance, when you believe the holder of e-mail, compound aspects such as what occurs when you propel an e-mail, the procedure your e-mail server utilizes are concealed from the customer. Consequently, to propel an e-mail you now require to type the satisfied, state the address of the receiver, and clicking send.

similarly, in Object-oriented indoctrination, abstraction is a course of hiding the execution aspects from the customer, only the functionality will be afforded to the customer. In other words, the customer will know what the thing does rather than how it does it.

In Java, abstraction is attained using Abstract classes and interfaces.

Abstract Class

A class, which contains the abstract keyword in its declaration, is recognized as an abstract class.

  • Abstract classes could or never enclose abstract processes, i.e., methods lacking body ( public void get(); )
  • However, if a class has at slightest one abstract technique, then the class ought to be stated abstract.
  • If a class is announced conceptual, it never is instantiated.
  • To utilize a conceptual class, you have to be left it from another group, supply completions to the abstract techniques in it.
  • If you succeed in a conceptual class, you have to supply accomplishments to all the abstract systems in it.

Example

This section supplies you with an instance of the abstract class. To generate a conceptual class, just utilize the abstract keyword ahead of the class keyword, in the class declaration.

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

You can view that excluding conceptual techniques the Employee class is identical to the regular class in Java. The class is currently conceptual, but it unmoving has three grounds, seven processes, and one constructor.

Now you be able to try to immediate the Employee class in a subsequent way:

/* File name : AbstractDemo.java */
public class AbstractDemo {

   public static void main(String [] args) {
      /* Following is not allowed and would raise error */
      Employee e = new Employee("George W.", "Houston, TX", 43);
      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
   }
}

When you compile the above class, it gives you the following error:

Employee.java:46: Employee is abstract; cannot be instantiated
      Employee e = new Employee("George W.", "Houston, TX", 43);
                   ^
1 error

Inheriting the Abstract Class

We can inherit the properties of Employee class just like a concrete class in the following way −

Example

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

Here, you never instantiate the member of staff class, except you can immediately the Salary Class, and with this example, you can contact entirely the three fields and seven techniques of Employee class as exposed below.

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

This produces the following result:

Output

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.0

Abstract Methods

If you desire a class to enclose a particular technique excluding you then want the definite accomplishment of that process to be concluded by child groups, you can announce the process in the parent class as a conceptual.

  • Abstract keyword is employed to state the technique as abstract.
  • You contain to put the conceptual keyword by the method name in the way statement.
  • A conceptual technique holds a method mark, except no method body.
  • Rather than curly supports, a conceptual technique will have a semi colon (;) at the end.

Subsequent is an instance of the conceptual method.

Example

public abstract class Employee {
   private String name;
   private String address;
   private int number;
   
   public abstract double computePay();
   // Remainder of class definition
}

Declaring a technique as conceptual has two results:

  • The class enclosing it must be stated as conceptual.
  • Any class that accedes to the recent class should either override the intangible method or state itself as intangible.

Note − eventually, a successor class has to apply the intangible technique; otherwise, you would encompass a ladder of conceptual classes that never be instantiated.

Presume Salary class come into the Employee class, then it should apply the computePay() technique as shown below −

/* 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;
   }
   // Remainder of class definition
}

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.