Click here to Skip to main content
15,887,892 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi.....

Can anybody explain me about interfaces in a very simple and descriptive manner. I know that interfaces is a substitute for multiple inheritence. But how interfaces saved from diamond problem and ambiguity error..Please explian in theory and practical manner both.
Posted

What is an Interface? @ oracle.com[^]

to complete that: it's a representation of an object.

So for example:

You want to provide the possibility of making input to your system - you could grant complete access:

Java
public class MyObject(){
  public myobject(){
  }

  public SecretData returnSecret(){
    return oSecretData;
  }

}


But that's not the best idea. You better create an interface just representing the functions the other side needs:

Java
public Interface IMyObject(){
  
 public OfficalValue getOfficalValue();

} 


public class MyObject() implements IMyObject{
  IMyValue oMyValue;
  public myobject(){
    oMyValue = this;
  }

  public IMyObject grantAccess(){
    return oMyValue;// interface
  }

  private SecretData returnSecret(){
    return oSecretData;
  }

  @Override
  public OfficalValue getOfficalValue(){
    return oOfficalValue;
  }
}



An interface can also be a common base for a variation of objects. The interface, which all objects are implementing is the guarantee to have the same access to all of the objects.
In this example all Vehicles are able to park and drive - only the tractor is able to pull a trailer:

Java
public class RoadTraffic{

   public void moveVehicles(Object oParticipatingObject){
     if(oParticipatingObject instanceof IVehicle){ // making sure it's a Vehicle
      IVehicle oVehicle = (IVehicle) oParticipatingObject;
      oVehicle.drive();
      if(oVehicle instanceof Tractor){
        ((Tractor)oVehicle).pullTrailer();
       }
     }
   }
}



public Interface IVehicle{

  public void park();
  public void drive();

}

public class Car implements IVehicle{

  public void park(){ }
  public void drive(){ }
}

public class Tractor implements IVehicle{
  public void park(){ }
  public void drive(){ }

  // addition function
  public void pullTrailer(){

  }
}




Other good uses are to store static values in there - like ID's and common used values that are used in different objects:
Java
public interface IConstants{
  public static final String 
     GEAR = "",
     LIGHT = "",
     ...
}

public class MyObject{

  public void drive(){
    if(IConstants.LIGHT.equals(...){
      // dofunnythings();
    }
  }


}


I hope I didn't miss to much and didn't write to much fails in here.
 
Share this answer
 
v3
Comments
Mehdi Gholam 7-Sep-11 10:56am    
Nice, 5!
Muhammad Faheem Ansari 9-Sep-11 2:31am    
can you explain with a very basic example...explain why do we need interfaces and the place where interfaces is the only solution...also compare it with the multiple inheritence..THANKS FOR POSTING...
TorstenH. 9-Sep-11 13:01pm    
There is never an single solution - you always have the choice. Some are better, some are less good. Some are even crab.

Examples are given above, 3 different uses for Interfaces. If you check the given link to oracle.com you'll find another example.
Muhammad Faheem Ansari 12-Sep-11 3:01am    
ok i agree that at someplaces multiple inheritence is good or sometimes interfaces is good...as you know multiple inheritence posseses ambiguity error and diamond problem and all that type of errors....now my question is how interfaces resolve this problem like ambiguity and diamond problem...this is what i am focusing on...although there is a solution in multiple inheritence too....but i want to know more about interfaces...please tell me in a simple and basic way...THANKS FOR POSTIMG...
Is this an interview question?
Search google, you will find many helpful links explaining about interface.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900