Thursday, 14 January 2016

What is Abstraction and What is abstract class & example


Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Abstraction in java is achieved by Interface and Abstract class.
- We cannot create instance of  abstract class.
 - Abstract class must be extends and it's method must be implemented 
 - Abstract class must have atleast on abstract methods.
 
An abstract class is a class that is declared abstract
it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

Imp points:
1)  We cannot create instance of Abstract class using new operator.
2) Abstract is a keyword in java which can be used for class and method
3) Abstract method doesn't have body,just declaration
4) We can't declare a variable as 'Abstract' .
5) Abstract class can also extend another Abstract class
6) If there is any Abstract method in class, that class must be Abstract class.


Sample code:
package oops;

abstract class Bank
{
    abstract int rateofInterest();
}
class SBI extends Bank
{
    int rateofInterest()
    {
        return 7;
    }
}
class KMB extends Bank
{
    int rateofInterest()
    {
        return 8;
    }
}

 class AbstractonTest
{
    public static void main(String[] args)
    {
        Bank b=new KMB();
        int interest=b.rateofInterest();
        System.out.println("rate of interest =" + interest);
    }
}


Output:
rate of interest =8

No comments:

Post a Comment