INTRODUCTION:
Hey friends.. Welcome Back.. In this post we are going to
look at the Interface and the reasons for us to use the Interface In the java
programs...
WHAT IS AN INTERFACE..??
Consider your sweet home.. Won’t we like to be well organized ..?? Like in an orderly manner.. well suited for our daily life..??
When we are in a hurry to set out of home.. we automatically
pick up the routine things from the place we have allocated for it..!! For example..
wallet must be in the cupboard and shoes mustn’t be thrown some where else. We
expect it to be in shoe rack.. !!
Well .. this organized things makes life easier.. We keep the
things we use in a routine manner in a some where accessible place and rarely accessible
things in some where inside our home..!! Don’t we..??
Of course.. we do..!!
Ok.. now..what is the connection between this and the
Interface..??
Well.. in INTERFACE we declare the methods and variables we
are going to use it very often.. so that it need not be declared in each and
every class.. !!
Sounds technical ..??
Don’t worry.. lemme show you an example..
Class Guitar
{
void play ( )
{
System.out.println(“I am playing guitar”);
}
public static void main(String args[])
{
Guitar g= new Guitar(
);
g.play();
}
Let us create another class and name it as violin
Class violin
{
void play ( )
{
System.out.println(“I am playing violin”);
}
public static void main(String args[])
{
Violin c= new Violin( );
c.play();
}
}
Now imagine we are supposed to create the play method in
about 10 classes . We have to manage it too !! So, now comes the argument
JVM yells at the developer.. .
JVM : Hey man ..
can’t you declare the methods you wish to use in an organized place so that I
will implement the methods automatically
in all the class..??
DEVELOPER : Oh.. Is it..?? But how will you know for which
class you got to implement the methods that I have declared in some ORGANISED
PLACE ..??
JVM : Hmm… Good question.. Let me explain you my dear..
We the machine, call the organized place as an INTERFACE..
where we let you ie. The developers put the stuffs you wish to see in many
class.
Then we usually will implement the methods , variables that
have been declared in INTERFACE (organized place ) for the class that
implements INTERFACE..!!
DEVELOPER: Oh.. man
you are confusing..!! What are you trying to say ?? In short ..
the stuffs we regularly use will be inside the INTERFACE and which ever
class implements INTERFACE , for that particular class, you will implement the
variables and methods that is inside INTERFACE .. !! Am I right ??
JVM : Well said
developer.. You learn how to use it.. Then you will understand the importance
of using it.
DEVELOPER : Ok..
dude.. thanks for the explanation.. !! I am seriously glad to have you by my
side..
So, story stops here.. let’s jump to the example and see for
better understanding..
STEP 1:
Create an interface by right clicking the package and select
create an INTERFACE
public interface stuffs
{
void play( );
}
STEP 2:
Create a class Guitar and let that class implement the
interface
Class Guitar implements stuffs
{
void play( )
{
System.out.println(“I am playing guitar “);
}
}
STEP 3:
Create a class Violin and let that class implement the same
interface
Class Violin implements stuffs
{
void play( )
{
System.out.println(“I am playing violin “);
}
}
Class MainClass
{
public static void main(String args[] )
{
Guitar g = new Guitar( );
g.play ( );
Violin v = new Violin( );
v.play( );
}
OUTPUT:
I am playing guitar
I am playing violin
EXPLANATION:
Now .. wow.. !! What we have done here..?? we have just
created an interface and inside that interface we are declaring the method
“play “ .
Note: The method that is declared inside the INTERFACE must
be proceeded with a semicolon.
Else it will throw an error and We are not supposed to
create any method / variable definition inside the interface
After creating an interface , we are creating classes and
letting the class to implement the interface . So that class which implements
the interface must implement all the methods and variables that it declared
inside the interface, in this case, the play ( ) method.
IMPORTANT POINTS ABOUT INTERFACE :
A class can implement more than one interface. Consider if
the first interface has the method play and the second interface has the method
pause then the class which implements these 2 interface must implement the
methods play and pause for sure.
INTERFACE won’t work unless IMPLEMENTS keyword is used in
the class
Eg. Class Classname IMPLEMENTS interfacename
Be it the method or the variable.. Which ever stuff is
declared inside the interface must be proceeded with a semicolon.
Eg. Interface sample
{
void samplemethod ( ) ;
int a ;
}
Apart from the implemented methods/ variables from the
interface, a class can have normal methods/ variables too.
All the methods that is declared within an interface is by
default public and abstract.
Any variable that is declared inside the interface is by
default public,static and final
We are not supposed to declare variable inside the interface
as private or protected.
We can’t declare the method inside the interface as static, private or protected.
If you don’t provide any access specifier by default it is
public.
We can’t declare constructors within an interface
We can’t create any CONCRETE METHODS inside an interface .
In short, a method with full definition is called as CONCRETE METHODS
Eg . void add ( )
{
int a= 9;
int b= 9;
int sum = a + b;
}
This add ( ) is the CONCRETE METHOD whereas if we say void
add( ); alone then it is method declaration.
So we can’t use concrete method inside an interface
Interface contains ABSTARCT METHODS. The method declaration
inside the interface is called abstract methods and the methods with its full
definition is called Concrete Methods.
We all know a class can EXTEND another class. ie during
inheritance. If you want to know about inheritance please refer this
link Inheritance in JAVA
A class can extend a class.. and similarly an interface can
extend another interface. When it does so,
All the methods in both the interface must be available in the class that implements it.
A class can implement an interface but an interface cannot
implement a class.
We cannot execute the interface alone .
USES OF INTERFACE :
Interface allows us to implement common behavior in
different classes. The implementation can be entirely different.We saw in the
above example, when a class implemented a method play ( ) , it was printing
something and other class printed some other. So We can have different
implementation.
WHY INTERFACE CAME TO BEING ..??
Well.. JAVA doesn’t support MULTIPLE INHERITANCE ..
When a child has multiple parents its multiple inheritance. Since
this feature isn’t there in JAVA.. INHERITANCE came to being to fulfill this.
INTERFACE does the same job.. !! A class can implement more
than one interface.
EXAMPLE:
public interface dad
{
void sweet( );
}
public interface mum
{
void caring ( );
}
Class child implements dad , mum
{
void sweet ( )
{
System.out.println(“I am sweet “);
}
void caring ( )
{
System.out.println(“I am caring”);
}
void childmethod( )
{
System.out.println(“I am not implemented method .. I am the
method from child class”);
}
public static void main(String args[] )
{
Child c = new Child ( );
c.sweet( );
c.caring();
c.childmethod();
}
OUTPUT:
I am sweet
I am caring
I am not implemented method .. I am the method from child
class
EXPLANATION:
As you can see, there is a single class child that is
implementing two interface dad and mum and all the method that is present
inside the interface must be implemented and even the child class is having its
own method too..!!
So , in short multiple inheritance is satisfied by means of
INTERFACE.
CONCLUSION:
So, folks.. thanks for reading this post.. Raise your
collars and clap your hands if you have understood this concepts better.. !!
Feel free to drop your comments below. Request for the post you wish to learn
is highly welcomed. Hit like button and help me post more. Thanks and have a
great day ahead J