1

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?

1
  • Thanks for accepting! Hope that explanation helped! Commented Feb 15, 2014 at 20:48

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As much as I got from the first answer this one definetly answered my question with no room for error in my head about being on the right track. I just wanted to know if I truly understood the concept and I will make a simple project just to practice this thanks a lot everyone and God bless :) :)
1

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.

Comments

0

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

5 Comments

So was I on track with what I am asking because I need a way to understand it in my own head. I keep seeing other ways to understand it but sometimes I have to get close to really get it?
what do u mean by psuedocode. also u can google more abt it.I dnt say rght now that u r on track or not
Ok I kind of get it, but how is List an Interface?
just open the list class at java.util.list and you will get the answer.Also first understand the imp of interface then it will help u alot
Psuedocode is like writing your program in plain English, they teach this in college however some programmers just use flow charts instead. example method dosomething is going to make a class of person clap their hands. :)
0

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.

Comments