Interfaces in JAVA


  • The interface is a concept by which we can achieve full abstraction in java.
  • Abstraction is a process of hiding the implementation details and showing only functionality to the user. In other words it shows only important things to the user and hides the internal details
  • By means of interface we are able to achieve multiple inheritance.
  • Interface should be declared with interface keyword and interface name

Ex: interface PrintableInt

  • We can nest Interface inside an Interface.
  • We can have abstract methods in the interface.
  • Interface can extend another Interface.
  • Inside a interface class we can only have abstract methods.
  • Interface takes the method declared as public abstract method by default
  • Interface takes the data members as public,final static by default.

Why do we need Interface

Imagine that you have a project for a Pet animal shop. Here you have an animal class a dog class and a cat class. Now you can create object for Dog and Cat class which means it has a behavior. For example dog it should bark sound and cat should give Meaw sound. But what does animal give. Does this make sense. No so in order to overcome this problem we go for this abstract class where the compiler will not allow us to create object. So in this case Abstract will help

But you have categories as Pet Animal shop, Animal shop and Robot Animal Toy shop. Here if you have animal class and Pet class that have same methods. In your project you want to have multiple inheritance but compiler will not allow you to do that. If compiler allows you to do that, your program will suffer from Death Diamond Problem. In order have multiple inheritance and full abstraction we go for this Interface concept.

Deadly Diamond of Death problem

Consider a example that you have a sub class as ComboDrive now we have got both run method in both super class. By this compiler gets confused which superclass to call. But we have a requirement that we need multiple inheritance. In order to overcome our requirement there is interface concept to help us

A simple program with interface and abstract class

interface Pet

{

abstract void wow();

}

abstract class Animal

{

public static void main(String args[])

{

System.out.println(“First Interface “);

}

}

This is a basic program for just interface syntax

A simple program with interface,multiple inheritance and abstract class

interface PetAnimal
{
abstract void grow();
}
interface Animal extends PetAnimal
{
abstract void grow();
abstract void move();
}

abstract class Dog implements Animal, PetAnimal
{
public void grow()
{
System.out.println(“Ceasar is my pet and it grows”);
}
public void move()
{
System.out.println(“Ceasar will walk around and Caesar moves quick”);
}
}
class InterSecond extends Dog
{
public void grow()
{
System.out.println(“Ceasar is my pet and it grows”);
}
public void move()
{
System.out.println(“Ceasar will walk around and Caesar moves quick”);
}

public static void main(String args[])
{
InterSecond t=new InterSecond();
t.grow();
t.move();
System.out.println(“First Interface “);
}
}

A simple program with interface,multiple inheritance and abstract class tagged Interface or marker Interface

interface PetAnimal

{

abstract void grow();

}

interface Geanny//tagged Interface for serializable, cloneable, Remote

{

}

interface Animal extends PetAnimal

{

int i=10;

void grow();

abstract void move();

}

abstract class AnimalQualities implements PetAnimal, Geanny, Animal

{

public void grow()

{

System.out.println(“Animal will grow “);

}

public void move()

{

System.out.println(“Animal moves”);

}

}

class Dog extends AnimalQualities

{

public void grow()

{

System.out.println(“Ceasar is my pet and it grows”);

}

public void move()

{

System.out.println(“Ceasar will walk around and Caesar moves quick”);

}

}

class InterThiIntInsideInt

{

public static void main(String args[])

{

Dog dogObj=new Dog();

dogObj.grow();

dogObj.move();

System.out.println(“First Interface “);

}

}

About pacesettergraam

A good and realistic person

Posted on December 29, 2012, in coding, Java and tagged , , . Bookmark the permalink. 1 Comment.

  1. plz write o/p 4 above pgm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: