Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ACHIEVING ABSTRACTION BY INTERFACES IN JAVA

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 224
    Comment on it

    Abstraction is a concept which shows only important(essential) feature and hide background details. In other way, abstraction shows only those things to user which are important and hides the background details. In java we can achieve abstraction with the help of interfaces.

    Interfaces

    An interface is basically a kind of class. Like classes, interfaces contain method and variables but with a major difference, the difference is that interface define only abstract method and final fields. This means that interface do not specify any code to implement these methods and data fields. Interface contain only constants.

    In addition to what an abstract class offers, an interface may effectively provide multiple inheritance. A class may implement an unlimited number of interface but can only extend one superclass.

    Syntax :-

    interface InterfaceName
    {
    variables declaration;
    method declaration ;
    
    }
    

    eg:-

    interface show{  
    void print();  
    }  
    class Sub implements show{  
    public void print()
               {
                   System.out.println("Hello All");
               }  
    public static void main(String args[]){  
    Sub obj = new  Sub();  
    obj.print();  
     }  
    }  
    

    Output:

    Hello All
    

    Multiple inheritance by interface

    An interface may effectively provide multiple inheritance

    interface Area
    {
    float compute(float a);
    }
    
    class Square implements Area
    {
    public float compute(float a)
    {
    return(a * a);
    }
    }
    
    class Rect implements Area
    {
    public float compute(float a,float b)
    {
    return(a * b);
    }
    }
    
    class InterfaceArea
    {
    public static void main(String args[])
    {
    Square sqr = new Square();
    Rect rec = new Rect();
    Area area;
    area = Square;
    System.out.println("Area Of Square = "+ area.compute(10));
    
    area = tri;
    System.out.println("Area Of Rectangel = "+ area.compute(10,2));
    }
    }
    

    OUTPUT

    Area Of Square = 100.0
    Area Of Rectangle = 20.0
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: