Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: General question about Interfaces

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default General question about Interfaces

    If I create an interface called 'abc' with these 3 methods, public void display() ; public void print() ; public void sample() ; and I implement them into class 'def' and display them, why do I need an interface that does nothing? I can do all the coding in 'def' without using an interface. I'm just a beginner with Java so please excuse the simplicity of my question.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: General question about Interfaces

    For a single class interfaces aren't terribly useful, and you're right that it looks just like unnecessary overhead/boilerplate code.

    However, consider a few different classes:

    Car, Truck, Boat, Lawnmower, etc.

    These can all be driven, so you could create an interface Drivable with a method drive(x,y).

    You can still use each class separately and "ignore" the fact that each class is Drivable. A car could still have a drive method even if it didn't implement the Drivable interface, and same with Truck, Boat, etc. However, what implementing Drivable does is it forces these classes to have this drive(x,y) method, so the user of these classes can treat them all as Drivable objects.

    This means we can do something like this:

    ArrayList<Drivable> myVehicles = new ArrayList<Drivable>();
    // let's add a few Drivable items
    myVehicles.add(new Car());
    myVehicles.add(new Truck());
    myVehicles.add(new Lawnmower());
     
    // for each vehicle, drive them home
    for(Drivable d : myVehicles)
    {
        d.drive(0,0);
    }

    Without the use of interfaces/inheritance/abstraction, I would be forced to maintain separate lists of vehicles for each type of vehicle.

    ArrayList<Car> myCars = new ArrayList<Car>();
    ArrayList<Truck> myTrucks = new ArrayList<Truck>();
    // ...

    This is cumbersome and error-prone when all I want is have a single list of objects which are Drivable.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: General question about Interfaces

    why do I need an interface that does nothing?
    Strictly speaking the interface does not do nothing: it defines a type that can be referred to elsewhere in your code.

    In the array list example that type is just the thing for declaring a collection which is designed to hold any of multiple implementations of the interface.

    Even when you have a single class implementing an interface, if you have a suspicion that *later* there may be multiple implementations then declaring an interface enables you to later add more classes without changing the code that uses them. The trick is to have that other code use the interface, not the specific implementation.

    A variation of this is where you provide no concrete implementation of the interface. It may be that you want to provide yourself or other programmers with a library that does useful things based on an object's ability to display(), print() and sample(). In that case the interface is enough and the actual task of implementing it is left to the other programmers, or to yourself in some specific situation.

    ---

    Of course although an interface does do something, it may or may not be the case that you *need* the interface. That isn't an abstract question, but one that depends entirely on what you're doing.

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: General question about Interfaces

    Hello! Thanks for your reply. Being a beginner I have to ask you a simple question. What does this code do? Is there a method called Drivable in
    Arraylist? What does (new Car()); do? What does this mean? for(Drivable d : myVehicles) Finally, What does this do? d.drive(0,0); Thanks!

    ArrayList<Drivable> myVehicles = new ArrayList<Drivable>();
    // let's add a few Drivable items
    myVehicles.add(new Car());
    myVehicles.add(new Truck());
    myVehicles.add(new Lawnmower());

    // for each vehicle, drive them home
    for(Drivable d : myVehicles)
    {
    d.drive(0,0);

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: General question about Interfaces

    Oops, sorry for adding in extra syntax and features.

    ArrayList<Drivable> myVehicles = new ArrayList<Drivable>();

    Drivable is an interface. This declares an ArrayList which can hold anything which is Drivable. Car, Truck, and Lawnmower all implement the Drivable interface so myVehicles can hold them.

    for(Drivable d : myVehicles)
    {
    }

    This is the for-each declaration. It basically loops through all the items in myVehicles.

    d.drive(0,0);

    This calls the drive method for the Drivable object d. Here's an example implementation of Drivable:

    public Interface Drivable
    {
        public void drive(int x, int y);
    }

    It's not terrible important what this does, it's just a demonstration class.

    Here's a simpler version which does a similar thing:

    Drivable[] myVehicles = new Drivable[3];
    myVehicles[0] = new Car();
    myVehicles[1] = new Truck();
    myVehicles[2] = new Lawnmower();
     
    // for each vehicle, drive them home
    for(int i = 0; i < myVehicles.length; ++i)
    {
        myVehicles[i].drive(0, 0);
    }

  6. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: General question about Interfaces

    Thank you very much. I'm an old Cobol programmer (30+ years) trying to understand new technologies. I appreciate your help!

Similar Threads

  1. Question Regarding Interfaces
    By davidrobin in forum Object Oriented Programming
    Replies: 1
    Last Post: April 18th, 2013, 04:10 PM
  2. General Question; Need Ideas
    By clydefrog in forum Java Theory & Questions
    Replies: 2
    Last Post: March 28th, 2012, 04:42 PM
  3. General Question
    By Becca in forum Collections and Generics
    Replies: 6
    Last Post: November 3rd, 2011, 03:52 PM
  4. Just a general Java related question
    By javaisfun in forum Java Theory & Questions
    Replies: 3
    Last Post: October 25th, 2011, 07:21 AM
  5. Newbie: general question on trying to get a simple Pokemon game to work
    By willmer in forum Object Oriented Programming
    Replies: 7
    Last Post: July 13th, 2011, 07:33 AM