8 Replies - 971 Views - Last Post: 02 April 2012 - 08:09 PM Rate Topic: -----

#1 bitpupil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-January 12

Java Interfaces

Posted 01 April 2012 - 01:10 AM

Hello guys. I have been studying Java Programming for the last 2 months using oracle's java tutorials. I have a decent understanding of Classes, Objects(class instantiations), Access modifiers, the hierarchical system in which Java arranges its parts and a bit of that basic stuff that you need to get familiar with before you delve into programming apps with the language

I was however unable to completely understand the application of Java Interfaces. I would have likde to paste-post an explanation of the topic by the tutorial am using but i think that would infringe on the copy rights of the author.

In a nutshell it explains an interface to be a contract between multiple programmers to adhere to an interface so as to allow their software products to interact.

An example would be a car manufacturing company publishing an interface which other companies or groups could use(implement?) to control the manufacturers car. These secondary parties would be such as a GPS technologies company which would like to produce a software product that collects data on traffic situations on roads that the 'driver' could use to get to his destination. The software would then be able to drive the car to the most favorable roads. The software would also be able to brake, accelerate the car when there was need to.(Its kind of a futuristic thing.)

The interface declares all the methods that can be invoked to control the vehicle. The code running under those methods is proprietary(and therefore secret) to the car manufacturer as is the code from the GPS company that invokes these same methods.

That is my understanding of interfaces. My questions then are;

1.) Who is implementing the interface, the Car manufacturer or the GPS company?

2.) The class that implements the interface has to include all the methods in the interface within its body then fill the methods with code. Given the GPS company writes this class, why would it fill the methods with code when they do not belong to it. I would expect the GPS company only to create objects that call the methods but not to fill the methods as they have already been filled by the car manufacturer. Whats left for the GPS Co. to do is to only invoke the methods if the GPS system finds need to.

3.)If indeed the GPS Co. doesn't implement the interface( i.e (2.) is not correct), how then would it be able to access the methods written by the Car manufacturing company?


Those are my questions. please make your responses as simple to understand as possible as English isn't my first language.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Interfaces

#2 varunit  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 03-September 09

Re: Java Interfaces

Posted 01 April 2012 - 01:34 AM

Think of interfaces as roles that could be played...

For example : Think about a printer. The Printer class implements Printable interface and as a matter of fact, any class can implement Printable interface (methods in the interface)


You have a class named Piano and a class name Printer and you want the printing code to be reusable, so that Piano class can also make use of it.

One way of doing it is through inheritance => Put the code for printing in the Printer class and let the Piano extend it, but it doesn't make sense to say, Piano IS-A Printer,

Therefore to achieve this, we pull out the reusable code and put it in an interface Printable and now any class can implement this interface which means to play that printing role... :)
Was This Post Helpful? 3
  • +
  • -

#3 bitpupil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-January 12

Re: Java Interfaces

Posted 01 April 2012 - 08:52 AM

Thank you for that explanation. Please give me another example just to make it clearer.
Was This Post Helpful? 0
  • +
  • -

#4 wordswords  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 272
  • Joined: 17-December 11

Re: Java Interfaces

Posted 01 April 2012 - 12:51 PM

An interface is just a contract to say 'classes that implement this interface will have these specified methods and will return these specified values'. So you can have an 'Edible' interface that a Burger and Fries classes would implement. This 'Edible' interface specifies that all 'Edible' objects have to have an 'eat()' method. It also specifies that the calling class gets a set number of calories when you call the method 'eat()'. So:

Edible.java
public interface Edible{

   public int eat();
}



Burger.java
public class Burger implements Edible{

   public int eat(){
      return 400;
   }
}



Fries.java
public class Fries implements Edible{

   public int eat(){
      return 250;
   }
}




If either 'Burger' or 'Fries' didn't include the 'eat()' method properly, a java compiler error would occur, because they declare themselves as implementing the Edible interface. The 'Burger' and 'Fries' classes are signing up to the 'Edible' contract, so they have to meet its requirements.

This post has been edited by wordswords: 01 April 2012 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Interfaces

Posted 01 April 2012 - 01:01 PM

Later on you can have a method that accepts an Edible as parameter

void displayCunt(Edible e) {

    // I don't have a slightest clue if e, receives as parameter, is a Fries or a Burger
    // I might don't even know that Fries an Burger exist
    // and actually I don't care
    // but I know that I can do

    System.out.println("Count: " + e.eat());
}


Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Java Interfaces

Posted 01 April 2012 - 01:05 PM

View Postpbl, on 01 April 2012 - 04:01 PM, said:

Later on you can have a method that accepts an Edible as parameter

void displayCunt(Edible e) {

    // I don't have a slightest clue if e, receives as parameter, is a Fries or a Burger
    // I might don't even know that Fries an Burger exist
    // and actually I don't care
    // but I know that I can do

    System.out.println("Count: " + e.eat());
}


Freudian slip? :nono:
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10443
  • View blog
  • Posts: 38,678
  • Joined: 27-December 08

Re: Java Interfaces

Posted 01 April 2012 - 02:03 PM

japanir's Abstract classes vs. interfaces tutorial is another good resource to check out.
Was This Post Helpful? 1
  • +
  • -

#8 bitpupil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-January 12

Re: Java Interfaces

Posted 02 April 2012 - 12:06 PM

To all, I will check out the references provided but in your own words what is it that Interfaces mean to achieve?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Interfaces

Posted 02 April 2012 - 08:09 PM

look at replies #4 and #5
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1