user3035444
user3035444

Reputation:

Understanding Interfaces Better

As I looked at many of the interface answers from questions here, and on Google and on this video class tutorial I am looking at I have a question. I am asking here because I can't comment if my reputation is not high so hopefully this is not to redundant. I am understanding that interfaces is like psuedocode but with more of an actual way to implement your psuedocode into the program. I undertsand

public Interface someInterface{ 
   public void doSomething();  
}

is like saying we need that function in our program so lets make this interface so when we do this

public class imDoingSomething implements someInterface{ // looking at the implements someInterface
   @Override  // optional 
  public void doSomething(){

    System.out.println("Doing Something");
  }
}

it makes sure as I write my program I don't forget to write this function for it is vital to my program. Is this correct?

Upvotes: 1

Views: 398

Answers (4)

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

In your example you have correctly implemented an interface. An interface can be viewed as a contract that a class must fulfill. Knowing that the class has met the requirements specified by an interface allows the object to used as the interfaces type by client code and guarantees particular methods will exist with a specified signature. This can make code more abstract and reusable for a variety of types.

So if we have an interface Playable:

public interface Play{
   public void play();
}

And two classes implementing Playable:

public class Record implements Playable{
   public void play(){
       System.out.println("Playing Record");
   }
}

public class MP3 implements Playable{
   public void play(){
       System.out.println("Playing MP3");
   }
}

They can be used in an abstract manner by a client because it knows all classes implementing Playable have a play method:

public class Application{

    List<Playable> audioFiles = new ArrayList<Playable>();

    public static void main(String[] args){

        audioFiles.add(new Record());
        audioFiles.add(new MP3());

        for(Playable p: audioFiles){
            play(p);
        }
    }

    public static void play(Playable playable){
        playable.play();
    }
}

On a side note

Follow Java naming standards when creating classes or interfaces. In Java these types use a capital letter for each word in the name. So your example would have a SomeInterface interface and a ImDoingSomething class.

Upvotes: 1

Andres
Andres

Reputation: 10727

Interfaces are a way of enforcing design restrictions. By declaring the type of a variable or parameter as an interface, you're sure that the instance referenced by that variable or parameter is going to have an implementation for every method of the interface. That's the basis of polymorphism.

Upvotes: 0

Smutje
Smutje

Reputation: 18173

It's more easy if you see interfaces from a consumer perspective - when you have a class which uses other objects and does not care about how these objects are concretely defined but only how these objects should behave, one creates an interface providing the methods and using it internally - and everyone which wants to use this certain class has to provide access to his data through implementing the interface so that the class knows how access everything on a code level.

Upvotes: 1

Kick
Kick

Reputation: 4923

An interface is a collection of abstract methods[No defination]. A class implements an interface, thereby inheriting the abstract methods of the interface.With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public.

You can not instantiate an interface - you can instantiate one of their subclasses/implementers.

Examples of such a thing are typical in the use of Java Collections.

List<String> stringList = new ArrayList<String>();

List is interface but the instance itself is an ArrayList

Upvotes: 0

Related Questions