Thursday, January 24, 2013

Abstraction in java

Abstraction is ability to make the class abstract in OOP.
As you know  abstract class we can not be instantiates so we can not instantiate object of that that class.
So her if class is not instantiated then its not much useful but to make it worth we must have to create subclass which makes this abstract class useful and where abstraction concepts comes into picture.

Lets say,

public abstract class Employee
{
      public String name = "Jignesh";
      private String salary = "1000";
}

So here we can instantiate object of this class like ,

Employee e = new Employee();

But we can create subclass like,

public class Salary extends Employee {

}

And can access particular variables and methods from abstract class.


 Abstract Method :

We can also declare abstract method which means we can put simply signature entry of that method in abstract class and can put real implementation in subclass which cause the abstraction concepts become true in programming world.

To make method abstract, you must have to declare that method as abstract in abstract class.

like,
public abstract class Employee
{
 public abstract int mySal(); 
}
 
and in subclass you can implement that method :
 
public class Salary extends Employee
{
   int salary =  1000;
  
   public double myPay()
   {
      System.out.println("Computing salary pay for " + salary);
     
   }

} 
 

No comments:

Post a Comment