Translate

Thursday, 19 June 2014

Abstract Class in Java

Abstract class in Java

If a class contains any abstract method,that class is known as abstract class. Abstract class is never instantiated.It is used to provide abstraction. Although abstract class does not provide 100% abstraction because it also contains concrete methods.

Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.

Abstract Method:The method inside the abstract class which have no body but its implementation must be provided in its subclass is known as abstract method. Abstract method never be final or static.

There are two ways to achieve abstraction
  1. Abstract class -0 to 100%
  2. Interface - 100%
Example of Abstract Class


abstract class Bike{  
         Bike()
     {
 System.out.println("bike is created");
     }  
        abstract void run();  
        void changeGear()
                       {
                           System.out.println("gear changed");
               }  
 }  
  
 class Honda extends Bike{  
                   void run()
 {
    System.out.println("running safely..");
 }  
 }  
 class TestAbstraction2{  
     public static void main(String args[]){  
          Bike obj = new Honda();  
          obj.run();  
          obj.changeGear();  
 }  
}  
output:
       bike is created
        running safely..
        gear changed

No comments:

Post a Comment