ABSTRACTION IN JAVA

 

 

Abstraction  in  java  is  a  core  principle  of  Object-Oriented  Programming (OOP)  language  which  is  used  to  hide  the   internal  complex  implementation  details  and  show  only  the  necessary  functionality  &  features  to  the  user . 

 

Java  Abstraction  focuses  on   what  an  object  does  rather  than  how   it   does  it   which  siplifies  the  code  organization  &  simplify the  application  system.

 

 

 

 

 

Some  Key   Characteristics   of  Java Abstraction:

 

  • Java  Abstraction  hides  the  complex  internal  implementation  details  and  shows  only  essential  functionality  to  the  user .
  • The  abstract  method  present  in  the  abstract  class  must  be  implemented  by  subclass.
  • Abstract  class  can  contain  both  abstract  and  non abstract  method . Abstract method  of  abstract  class  must  be  implemented  by  subclass.
  • Java  abstraction  define  common  blueprints ,  which  allows  developers  to   reuse  shared  behavior  across  different  unrelated  classes.
  • Abstraction  restricts  unauthorized  access  to  data  by  hiding  internal  implementation  details .

 

 

 

How  to  achieve  Abstraction  in  Java?

 

 

In Java, Abstraction  can  be  achieved  in  two  mechanisms : abstract classes  and  interfaces.

 

 

 

  1. Abstract  Classes :

 

 

Abstract  class  in  Java  is  a  class  which  cannot   be  instantiated   and  can  contain  both  abstract  &  non abstract  methods .

 

 

Real  life  example  of  Java  Abstraction:

 

 

Sending  Message By  Android Phone When  you  use  android  phone  and  send  message  to  another  person   you  just  type  the  message  and  send , but  you  don’t  know  the  internal  implementation  of  android messaging. This  is  real life  example of  abstraction  as  internal  implementation  is hidden showing  only  functionality  to   the   user .

 

 

The  Television  Remote  control :  The  television  remote  control  is  the  best  example  of  Java  Abstraction .  You  don’t  need  to  know  how   the  TV  internally  works ;  you  just  need  to  press  the  button  to  change  the  channel  .

 

 

 

 

Example:

 

abstract  class  Mobilephone{

 

abstract  void  calling();

abstract  void   messaging();

void  internet();

}

 

 

class  Test  extends  Mobilephone {

 

//You  must  override  the  abstract  method  of  abstract  class  in  subclass

 

@Override

 

 void  calling() {

   System.out.println(“Phone  calling  in  Test “);

      }

 

 

@Override

void  messaging () {

 

 System.out.println(“Phone  messaging  in  Test “);

 

      }

 

  }

 

 

class   DemoAbstractclass  {

   public  static  void  main  ( String  args[] ) {

 

      Mobilephone  m1 =  new   Test () ;

        m1.calling();

        m1.messaging();

}

 

   }

 

 

 

 

 

 

 

 

2. Interfaces : 

Interface  in Java  is  a  blueprint  of  a  class  which  is  used  to  achieve  100%  abstraction  in  Java.  

 

One   interface  can  extend  one  or  more  interface.

 

One  class  can  implement  one  or  more  interface.

 

 

 

 

Java  Interface  Syntax :

 

[access_modifier]  interface    InterfaceName  {

          //Contant  fields

         //Abstract  methods

        //  Default  methods (Java  8+ )

       //  Static  methods  (Java   8+ )

      //  Private  methods (Java 9+ )

    

       }

 

 

 

 

 

     Members  of  an  Interface :

 

              Interfaces  can  contain  various  memers  which  are  given  below :

 

                    Constant  Fields  :    All  variables (fields )  declared  in  java  interface  are  implicitly  public ,  static  and  final   which  must  be  initialized  at  the  time  of  declaration .

 

Example:

 

int    count=8;

public  static   final  int  count =8;

 

//In  the  above  example  both  statement  are  equivalent  i.e.  in  interface  by  default  all   fields  are  public, static  and  final.

 

 

 

 

          Java  Abstract  Methods :  Before  Java  8 ,  interface  could  have   only   abstract  methods .  These  methods  are  implicitly  public  and  abstract  which  have  no  body .

 

 

Example :

 

 void  calculation ( int  count ) ;

 

 public  abstract  void  calculation ( int  count ) ;

 

 

//In  the  above  example  both  statement  are  equivalent  i.e.  in  Java  interface  by  default  all  methods  are  public  abstract.

 

 

 

 

  Java  Static  Methods ( Java  8+ ) :    From  Java  8 , Inteface  can  have  static  methods  also  which  can  be  called  using  interface  name  and  they are  implicitly  public .

 

Example :

 

  static  void  details () {

     System.out.println(” This  is  an  interface  static  method “);

   }

 

 

 

Java  Private  Methods (Java  9+ ) :  Java  private  methods  can  be  used  to  share  common code  between   default  and  static  methods  within  the  interface    itself ,  but  cannot  be  accessed  by  subclasses.

 

Example :

 

private  void    calculation ( ) {

 

System.out.println(“Calculation  done”);

 

}

 

 

 

Java  Interface  Example :

 

 

interface     Calculation  {

 

//abstract  method  to  calculate

          double   calculate () ;

         

}

 

 

//Implement  the  Interface  in  a  class name  Addition

 

class   Addition    implements   Calculation {

      double  a;

      double  b;

 

//Constructor  for  Addition

public   Addition(double a , double  b ){

this.a=a;

this.b=b;

}

 

 

// implementing  the    abstract  method  of  interface  Calculation  

 

public    double  calculate ( ) {

         double  sum= a+b;

         return  sum ;

}

}

 

 

class  Multiply   implements  Calculation{

         double  c;

         double   d;

 

//Constructor   for  multiplication

 

      public  Multiply ( double  c ,  double  d ) {

       this.c=c;

       this.d=d;

 

     }

 

// implementing  the  abstract  method  of  interface  Calculation

 

  public  double  calculate () {

       double  mult =  c * d ;

       return  mult ;

    }

  }

 

      

 

 

 

 

 

public  class  Main {

          public  static  void  main (String  args [] ) {

  //Reference  type  of  the object  is  interface  Calculation  .   

 Calculation     sum =  new   Addition (28.0,34.0);

 

  Calculation  mult =  new  Multiply(8.0 , 18.0 ) ;

 

   System.out.println(“Calculated  sum  is : “ + sum.calculate( ));

 

  System.out.println(” Calculated  multiplied  value  is : “ + mult.calculate());

 

  }

 

 

  }

 

 

 

 

 

 

OUTPUT :

Calculated  sum  is :    62.0

Calculated  multiplied  value  is :  144.0

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *