Saturday, 28 January 2017

Abstraction in Java

Abstraction in Java

It is the process of hiding the implementation details and showing only functionality to the user.

For example sending sms, you just type the text and send the message. You don't know the internal processing about the message sending and delivery.

Abstraction lets you focus on what the object does instead of how it does.


There are two ways to achieve abstraction in java
- Abstract class (0 to 100%)
- Interface (100%)

Example:
abstract class Shape
{
    abstract void draw();
}
class Circle extends Shape
{
    void draw()
    {
        System.out.println("circle is drawing");
    }
}
class Square extends Shape
{
    void draw()
    {
        System.out.println("square is drawing");
    }
}
class Demo
{
   public static void main(String args[])
    {
        Shape s1=new Circle();
        s1.draw();
        Shape s2=new Square();
        s2.draw();
    }   
}

Output:
mukesh@ubuntu:~/Desktop/javaPrograms$ java Demo
circle is drawing
square is drawing

Point to be noted :

1. Abstract method : No definition just declaration,all declare method should have abstract keyword
2. If class containing any abstract method ,class should be declare as abstract class.
3. we cant create an object of an abstract class but can create just reference variable.(C c1=null;),
4. null is keyword which is used to initialize any reference variable
5. An abstract method can be use by inheritance and on creation of object.
4. Abstract class also generate .class file on compiling.


Programs for Practice on Abstraction

Program # 1

abstract class E
{
     abstract void test1();
     void test2()
     {
        System.out.println("Test2");
     }
}
class Demo extends E
{
     void test1()
     {
          System.out.println("Test1");
     }
     public static void main(String[] args)
     {
        Demo d1=new Demo();
          d1.test1();
          d1.test2();
          System.out.println("Done");
     }
}

Output::
Test1
Test2
Done

Program # 2
abstract class G
{
     void test1()
     {
          System.out.println("Test1");
     }
     abstract void test2();
     void test3()
     {
        System.out.println("Test3");
     }
}
class H extends G
{
     void test2()
     {
          System.out.println("Test2");
     }
     void test4()
     {
          System.out.println("Test4");
     }
     public static void main(String[] args)
     {
          H h1=new H();
          h1.test1();
          h1.test2();
          h1.test3();
          h1.test4();
          System.out.println("Done");
     }
}

Output::
Test1
Test2
Test3
Test4
Done

Program # 3
abstract class J
{
     abstract void test1();
     abstract int test2();
     abstract int test3(double d);
     void test4()
     {
          System.out.println("Test4");
     }
}
abstract class K extends J
{
     void test1()
     {
          System.out.println("Test1");
     }
}
class L extends K
{
     int test2()
     {
          System.out.println("Test2");
          return 10;
     }
     int test3(double d)
     {
          System.out.println("Test3");
          return 20;
     }
}
class M
{
     public static void main(String[] args)
     {
      L h1=new L();
      h1.test1();
      h1.test2();
      h1.test3(2.3);
      h1.test4();
      System.out.println("Done");
     }
}


Output::
Test1
Test2
Test3
Test4
Done

Program # 4
Note: An abstract method can have all concrete method even though we can not create the object of an abstract class.

abstract class N
{
     void test1()
     {
        System.out.println("Test1");
     }

class O extends N
{
}
class P
{
     public static void main(String[] args)
     {
          N n1=new N();
          n1.test1();
          //O o1=new O();
          //o1.test1();
          System.out.println("Done");
     }
}

Output::
When try to create object of abstract class then following compilation error.
Test.java:15: error: N is abstract; cannot be instantiated
          N n1=new N();
               ^

Program # 5
Note: Whenever object creates constructor executes. We can keep a constructor inside a abstract class but constructor cannot be abstract.
      Constructor can not inherit to base class ,so we can not implement the abstract class hence constructor can not be abstract.
     
abstract class Q
{
     Q()
     {
          System.out.println("Q()");
     }
     abstract void test1();
     abstract void test2();
     void test3()
     {
        System.out.println("Test3");
     }

class R extends Q
{
     R()
     {
          System.out.println("R()");
     }
     void test1()
     {
          System.out.println("Test1");
     }
     void test2()
     {
          System.out.println("Test2");
     }
}
class S
{
     public static void main(String[] args)
     {
          R r1=new R();
          System.out.println("------");
          r1.test1();
          r1.test2();
          r1.test3();
     }
}

Output::
Q()
R()
------
Test1
Test2
Test3


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...