Monday, 4 September 2017

Abstraction in Java

In simple term we can say it hide unnecessary detail and show functionality .i.e there are student Class so it well hide unnecessary details like hair color ,skin color,and details of body parts.for student Class actually mean roll no., marks,address etc.
And show us functionality like countMarks(),CurrPerformance().

In java there are two ways to achieve Abstraction.
  • Abstract Class
  • Interface Class

An interface or abstract Class is something which is not actual, something which is unfinished.

 In order to use interface or abstract Class , we need to extend and implement an abstract method with concrete behavior. 

One example of Abstraction is creating interface to indicate common behavior without specifying any details about how that behavior works 

e.g. You create an interface called wi-fi Router which has the start() and stop() method. This is called abstraction of Server because every server should have a way to start and stop and details may be different.


Abstract Class

Abstract Class es are Class es that can have one or more abstract methods. An abstract method is methods that is declared, but have no implementation.

 Abstract Class es may not be instantiated, and need subClass es to give implementations for the abstract methods.

To declare Class or method as abstract keyword uses called “Abstract”


Example Code
abstract class Area1
{
    abstract double area(double a);
    void show()
    {
         System.out.println("method from abstract class");
    }
}

class Area2 extends Area1
{
    double area(double l)
    {
        return(l*l);
    }
}

class Area3 extends Area1
{
    double area(double r)
    {
        return(3.14*r*r);
    }
}

class Abstract1
{
    public static void main(String s[])
    {
                               
     Area2 obj2 = new Area2();
     double c = obj2.area(5.0);
     System.out.println("C= " +c);
     obj2.show();
     Area3 obj3 = new Area3();
     c = obj3.area(4.0);
     System.out.println("C= " +c);
   }
}

                               

Note that the abstract keyword is used to denote both an abstract method, and an abstract Class .



Interface Class

An interface is a point where two systems meet and interact. For example, t.v. remote
We can increase and decrease volume through remote.so in this case remote is interface to interact.

From a programming perspective, an interface meets between software components. 

Consider that a method header (method name, parameter list, and so on) interface meets between external code that calls the method and the code within the method that will be executed as a result of the call. Here is an example:


Code
interface Area1
{
    final double PI=3.14;
    public double area(double a);
    public void shape_name();
}

class Area2 implements Area1
{
    public double area(double l)
    {
        return(l*l);
    }
   public void shape_name()
   {
         System.out.println("Shape is square");
   }
}

class Area3 implements Area1
{
   public double area(double r)
   {
         return(3.14*r*r);
   }

   public void shape_name()
   {
         System.out.println("Shape is circle");
   }
}

class Interface1
{
   public static void main(String s[])
   {
                               
                Area2 obj2 = new Area2();
                double c = obj2.area(5.0);
                System.out.println("C= " +c);
                obj2.shape_name();
                Area3 obj3 = new Area3();
                c = obj3.area(4.0);
                System.out.println("C= " +c);
                obj3.shape_name();
    }
}


As you can see, an interface is declared using the Java interface keyword.

Just like with Class es, a Java interface can be declared public or package scope (no access modifier).
The interface example over contains one variable and one method. 
The variable can be accessed directly from the interface

1 comments: