Ticker

6/recent/ticker-posts

JAVA Interface with Example

Interface 

Interface in java is one of the way to achieve 100% abstraction in Java . 

Interface in java is declared using keyword interface. It is also just like normal class. For interface also the .class file is going to be generated. 

All methods inside interface are public by default 

All variables declared inside interface is implicitly public final variable or constants. 

interface Office 

        int employee;//Error because only final variables so they need to be initialised has donoted in below example. No other types are allowed inside interface 

 } 

interface Office 

        int employee=100; 

All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword 

Cant create the Object of interface 

In Java its legal for an interface to extend multiple interface. for example following code will run without any compilation error 

package com.jspiders; 
interface Animal 

            public void eat(); 
            public void walk(); 

interface Human 

            public void talk(); 
            public void work(); 

interface Livingbeing extends Animal,Human // extend multiple interface 

        public void enjoyWithLover(); 
}

 Whenever we implement interface we need to provide implementation to all the abstract methods of it, or else we need to make the inherited class also has abstract. 

interface Office 

        void work(); 

        public void enjoy(); 

 } 

abstract class Manager implements Office 

        //implementation is not give for work() and enjoy() so class should be abstract 

Example:- 

interface Animal 

        public void eat(); 
        public void walk(); 

interface Human 
{
         public void talk(); 
        public void work(); 

interface Livingbeing extends Animal,Human 

        public void enjoyWithLover(); 

public class Test implements Livingbeing

        public void eat() 
         { 
                // TODO Auto-generated method stub 
        
        public void walk() 
        
                // TODO Auto-generated method stub 
        
        public void talk() 
       
                // TODO Auto-generated method stub 
        
        public void work() 
        
                // TODO Auto-generated method stub 
        
        public void enjoyWithLover() 
        
                // TODO Auto-generated method stub 
        
}

The Interface reference can be give to its child class object. It behave same has the inheritance. Using the interface reference we cant accesses the child class members. But we can accesses the overrided methods. 

Example: 

interface Animal 

        public void eat(); 
        public void walk(); 

class Cow implements Animal 

        public void eat() 
        
                System.out.println("Cow eat hullu"); 
        
        public void walk() 
        
                System.out.println("Cow walk in four legs"); 
        
        public void giveMilk() 
        
                System.out.println("Cow give white milk"); 
        

class Test1 

        public static void main(String[] args) 
        
                Animal a = new Cow(); 
                a.eat(); 
                a.walk(); 
                a.giveMilk();//Error because parent reference cant accesses child members 
        

Difference Between Abstract class and interface in java

  Abstract Interface
Multiple Inheritance A class can inherit only one
Abstract Class
A class can implement multiple
Interfaces
Concrete methods (Default
Behavior)
In abstract class you can
provide default behavior of a
method, so that even if child
class does not provide its own
behavior, you do have a default
behavior to work with
You cannot provide a default
behavior in interfaces.
Interfaces only allow you to
provide signature of the method
Access Modifiers You can provide access
modifiers to methods in abstract
classes (static , final, etc)
You cannot provide access
modifiers methods in Interfaces.
Methods and variables The methods or variables can
be declared with any accesses
specifiers and modifiers. (Note :
but abstract methods should not
be static , final, private)
All methods are public by
default, all the variables are
static final by default.

ALSO SEARCH:

"java interface with example"

"when to use abstract class and interface in java with real time example"

"interface in java with example program"

"difference between class and interface in java with example"

"abstract class and interface in java with real time example"

"what is functional interface in java 8 with example"

"multiple inheritance in java using interface with example programs"

"what is the use of marker interface in java with example"

"multiple inheritance in java using interface with example programs pdf"

"interface in java with real time example"

"explain java interface with example"

"interface in javascript with example"

"write a java program to implement runnable interface with example"