Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Introduction of Errors
Java Exceptions
Java Try Block
Java Catch Block
Java Finally Block
Java Throw Keyword
Java Throws Keyword
Java Builtin Exceptions
User Defined Exceptions

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Interface


Interface in Java

Interface is also known as kind or type of a class. It contains only abstract methods and final fields.

Interface do not specify any code to implement methods or initialization of data. These are always implemented in the class which defines the implementation of these methods.


Characteristics of Interface

  • They contains only abstract methods and final fields (constants)
  • Interface can extends other interfaces but cannot define superinterface methods.
  • Interface can extends multiple interfaces seperated by commas
  • A class can also implement multiple interfaces seperated by commas
  • Interface cannot extend other classes.
  • Interface cannot be instantiated (object not created)
  • Interface does not contain any constructor like class.

Uses of interface

Interfaces are mostly used in java for following purposes

  • To achive the data abstraction
  • To achive the multiple inheritance

  • Syntax for creating interface

    interface InterfaceName
    {       
        // methods declarations                    
        return_type methodName1 (parameterList);
        return_type methodName2 (parameterList);
        .
        .
        return_type methodNameN (parameterList);
        
        
        // field declarations
        type final_field = value;
        type final_field2 = value;
        .
        .
        type final_fieldN = value;
    }
    

    Variables

    By default variables declared inside the interface becomes final fields. They must be initialized to constant value otherwise gives an error as the black field variable have not been intialized.

    Methods

    Interface contains only declaration of methods without writting its body with statements. By default declared methods are considered as a abstract methods. Easy class which implements that interface must write all methods body.


    Example of interface

    interface Mobile
    {
        int price = 5600;
        String brand = "MyPhone";
        void displayDetails();
        void startPhone();
    }
    
    class PhoneUser implements Mobile
    {
        public void displayDetails()
        {
    	System.out.println("\nPhone brand : "+brand);
    	System.out.println("\nPhone price : "+price);
        }
        public void startPhone()
        {
    	System.out.println("\nStarting the phone ...");
        }
    
        public static void main(String args[])
        {
    	PhoneUser n1 = new PhoneUser();
    	n1.displayDetails();
    	n1.startPhone();
        }
    }
    

    Output

    Phone brand : MyPhone
    Phone price : 5600
    Starting the phone ...

    In above example we have created interface Mobile, which is implemented in the class PhoneUser. All methods are implemented inside the class and called them using object of class.


    << Previous Next >>

    See Also

    All Java Programs
    Java Keywords
    Java Interview Questions

    AndroidBerry Support

    About us
    Contact us
    Suggest us
    Questions?

    Follow us

    Androidberry FacebookFacebook
    Androidberry TwitterTwitter
    Androidberry GooglePlusGoogle+
    Back to top
    Androidberry Name Logo