banner image
Sedang Dalam Perbaikan

Java Interfaces


Hi everyone! Have you read my previous articles on Java? I think you have read them. So in my last article published on Java, I discussed about Abstraction, a key concept in OOP. If you haven't ewad it, this is the link..
Java Abstraction

The topic of today's article is Java Interfaces.. Actually this is a concept connected to the Java Abstraction also..You remember last time I explained abstract classes? These Interfaces are built on it. Let's start the work.

What is an Interface? 

It's similar to a class. It's a blueprint of a class. When a class having its all methods in abstract form, then that class can be called as an interface simply. Do you remember? Abstract methods always don't have a body! These interfaces are used when we need to achieve abstraction and multiple inheritance. A class can be implemented using an Interface. Then the methods, behaviors of the interface are inherited to the particular class.
In OOP inheritance, we used a keyword called extends. But here we use implements keyword...extends keyword can not be used when a class is inherited from an interface.
Example for a Java Interface is given below..

interface Animal{
void eat();
void sleep();
}
class Dog implements Animal{
public void eat(){

}
public void sleep(){

}
}

Important Facts:


Fact 1 :

The methods in a Java Interface are always implicitly public and abstract. No need to mention specially the access modifiers. But they can not have a private access modifiers.

Fact 2 :

When we create classes implementing an interface, the overridden method can not have an access modifier which is less powerful than the interface method access modifier.

interface Animal{
// this means --> public abstract void eat();
void eat();
// this means --> public abstract void sleep();
void sleep();
}
class Dog implements Animal{
// Error: protected is a weaker access modifier
protected void eat(){

}
// No error: same level of access modifier - public
public void sleep(){

}
}

Fact 3 :

All the methods in the super interface must be overridden in the classed inherited from the super interface.

interface Animal{
void eat();
void sleep();
}
class Dog implements Animal{
public void eat(){
// method body
}
public void sleep(){
// method body
}
}
class Puppy implements Animal{
public void eat(){
// method body
}
public void sleep(){
// method body
}
}

Fact 4:

We can create reference variables from Interfaces. But cannot create OBJECTS...

interface Animal{
void eat();
void sleep();
}
class Demo {
public static void main(String args[]){
Animal a1; // No erros
Animal a2 = new Animal(); // Error
}
}

Fact 5 : 

If there are attributes in an interface, they become public + static + final attributes implicitly. So we can not do just a declaration for an attribute. It should be initialized always!

Fact 6 : 

Interfaces support for Dynamic Method Dispatch.. Example is given.

interface Animal{
void eat();
}
class Dog implements Animal{
public void eat(){
System.out.println("Dog is sleeping");
}
}
class Demo {
public static void main(String args[]){
Animal a1;
a1 = new Dog();
a1.eat();
}
}

Fact 7 : 

There can not be constructors in a java interface. So, the chance of having attributes also very low since we declare and initialize attributes within the constructor.

Look at the below possible uses of classes and interfaces...


Case 1 :

Both A and B can be classes or interfaces. Both should of in one form..
Example :

class A{

}
class B extends Animal{

}

interface A{

}
interface extends B{

}


Case 2 :

When there's an implementation in the middle, the child should be a class. Other one should be an interface.
Example :

interface A{

}
class implements B{

}

Case 3 :

Here all 3 components must be interfaces..There's NO MULTIPLE INHERITANCE in Java using classes. But still we can use interfaces for multiple inheritance. So a class can be inherited from only one super class.

This also a possible one...

class P{

}
interface Q{

}
class R extends P implements Q{

}


Look at this example. It will cover most of the things we discovered.


class P{
void pMethod(){
System.out.println("pMethod of class P");
}
}
class A extends P{
void aMethod(){
System.out.println("aMethod of class A");
}
void pMethod(){
System.out.println("Overridden pMethod of class A");
}
}
interface Q{
void qMethod();
}

class R extends P implements Q{
public void qMethod(){
System.out.println("Overidden qMethod of interface Q");
}

void pMethod(){
System.out.println("Overridden pMethod of class P");
}
}

class Demo{
public static void main(String args[]){
// Dynamic method dispatch happens
P r1 = new R();
r1.pMethod();
// only override
R r2 = new R();
r2.pMethod();
r2.qMethod();

P p1 = new A();
p1.pMethod();
}
}

Output :


This is the end of the tutorial. Hope you have got an idea about Java Interfaces..I explained the things I have learned so far. You can refer my other OOP articles also. If you are not familiar with this, please refer my Abstraction article first and then read this. IF you have any question, drop a comment.

Bye guys!!!




Java Interfaces Java Interfaces Reviewed by DAL on April 09, 2018 Rating: 5

No comments:

Powered by Blogger.