Java Abstraction:
1.Is one which make a class abstract in object orient programming. It means it provides only essential features for a certain program when needed.
2. Abstract class cannot be instantiated.
3.Abstract methods will be declared in parent class.
4.If a class has abstract method .Its class should also been declared as abstract.
Example:
{code type=”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;
}
}
{/code}
This above code is an example to abstract class,where abstract keyword appears before class keyword in class declaration.
5. Abstract Class can be inherited.
Abstract Method:
Abstract method is one where you can use whenever you look to define a method in a derived class.You can make that method as abstract in parent class with only declaration prefixed with keyword abstract.
{code type=java}
public abstract class Employee
{
private String name;
private String address;
private int number;
public abstract double computePay();
//Remainder of class definition
}
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
}
{/code}
Here in this above code computePay() is declared as abstract method in its parent class Employee.It's definition can be defined in any of its derived class.Here it has been defined in Salary derived class. Points to remember while using abstract method: 1.Class must also be declared as abstract if it contains abstract method. 2.Any Child class must override the abstract method declared in parent class.