blog

Home / DeveloperSection / Blogs / Introduction to Abstraction in java

Introduction to Abstraction in java

Hemant Patel 2303 19-Jan-2017

Abstraction:-  

Abstraction means show functionality and hide complexity. In object oriented programming functionality should be shown to user but complexity (code of the function) should be hidden for the user. It can be achieved by following two ways:-

1.Abstract class

2.Interface

Abstract class:-  

Abstract class can not be instantiated. It means object of an Abstract class can not be created. Abstract modifier should be use to define an abstract class.

e.g.:-

   abstract class Shape
         {
            }
           class Text
                       {
                         main method
                             {
                                 new Shape(); //Error because abstract class never create object
              }
     }


Abstract method:-  A method which does mot have body is known as abstract method. Abstract method should be defined by using abstract modifiers.

Rule:- Abstract method can only be defined in abstract class.

  It means if a class has any abstract method then that class must be an abstract class.

   e.g:-

abstract class Shape
{
abstract void findArea();
}

 Rule:- 1.    If parent of a class is abstract then all the abstract method of parent class must be overridden to this class(child class) otherwise you will have to make this class an abstract class.

2. Abstract class should be define to provide prototype of a method to all its type so that methods prototype in each child class should be same.

e.g.:-

    abstract class Calculator
{
          int num1,num2,res;
          void accept Number(int n1,int n2)
          {
          Num1=n1;
          Num2=n2;
          }
          abstract void cal();
          void showResult()
          {
          System.out.println(res);
          }
}

 Interface:- Interface also structure of class is should be define by using interface keyword.

e.g:- 

interface Automobile

{       

}

And //

    Class Test

     {

         main method

          {

           new Automobile();

          }

      }

And//

interface Automobile

{

methods();

}

Method of a interface by default public and can only be public.

Implementation of Interface:-

An Interface contains prototype of methods and thes methods will be define by classes.This is called Implementation of interface the class that implement in that Interface is called Implementation of class.

e.g:-     

class Bus Implements Autimobile
{
  public void method of parent class()
  {
  System.out.println(“method of parent class executed”);
  }
}

 All the method of interface must be overridden (Implemented) by child class.

All the overriding rules must be follow here

*Method of Interface can not be static.

e.g:-          

Type of a reference variable can be:-

1. The class which is instantiated

2. Parent class

3. Interface that implement

 class TestBus
          {
          Main method
          {
          Automobile auto=new Bus();
          Auto.start();
          Auto.stop();
}
          }

e.g:-

 class A
{
}
inteface B
{
}
class C extend A implements B
{
main method
  {
   C obj1=new C();
   A obj2=new C();
   B obj3=new C();
  }
}

You can also visit these related helpful link

What is the difference between abstraction and encapsulation

Can you use abstract and final both with a method in java

Difference between Abstraction and Encapsulation in Java

Abstraction in java


Updated 17-Mar-2018
Exploring the online world is my passion. have experience of #content writing #SEO #Digital Marketing #On-Page #Lead Generation #Content Analyst #Marketing Analyst... I could never stop at one point, continuously wanted to acquire more and more skills. My work for any organization will be full of passion and hard work.

Leave Comment

Comments

Liked By