Page 3 of 3 FirstFirst 123
Results 41 to 53 of 53

Thread: Interface?

  1. #41
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Thats it!!! I got it now... After I read maknib's last reply others' replies flashback on my mind and make understand everything they've said.

    Thanks maknib, Fubarable, Singing Boyo, pbrockway2 and JoKeR313 for explaining.

  2. #42
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Why interface and not class?

    As Singing Boyo says, you can only inherit from a single class, whereas you can implement any number of interfaces.

    That's a practical reason. Think Comparable. Now lots of things are going to be Comparable, but if you had a Vehicle class that's the basis of one tree (with Cra, Bicycle etc as child classes) then you might want that to be Comparable. But what if you also had an Animal class (with children like Dog, Cat etc) that you also wanted to be Comparable? Without interfaces you would force Vehicle and Animal to extend a Comparable base class.

    Now, what if you also wanted Animal classes to be able to doSomething()? DO you extend Comparable to add a doSomething() method? Then what happens if you also had a Building class that you wanted to doSomething, but not be Comparable?

    Put simply, interfaces provide you flexibility to define what a class can do. And what a class (and hence an Object) can do is the basis of OO programming.

    For your vehicle example you could stick with the basic set you define and also provide an EmergencyVehicle interface...say with a flashLights() and a soundSiren() method?

    ETA: If you go into development as a job, in Java, you'll find interfaces incredibly useful when it comes to testing. This is something the Spring framework uses a lot. If I have a class that uses a load of other services (ie classes) I can define those interfaces and provide setters for them in my class.
    Java Code:
    public class MyClass {
        private MyService ms;
    
        public String processSomethingOrOther(String someData) {
            if (ms.someCheck(someData)) {
                return "check passed";
            } else {
                return "test failed";
            }
        }
    
        public void setMS(MyService ms) {
            this.ms = ms;
        }
    }
    MyService is an interface defining some methods, like someCheck().
    With the above I can now write a test for MyClass to make sure that processSomethingOrOther() returns the correct data in different situations. As part of the setup for that test I can provide a Mock implementation of MyService, which will simply provide the results I want (true or false for someCheck() in this case). This is handy because the "proper" MyService implementation might hit a database, and ensuring that the data there is correct is a pain and, in any case, it's not MyService I'm testing, it's MyClass.

    Just thought I'd throw that one in.
    Last edited by Tolls; 11-05-2010 at 08:52 AM.

  3. #43
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Why interface and not class?

    As Singing Boyo says, you can only inherit from a single class, whereas you can implement any number of interfaces.

    That's a practical reason. Think Comparable. Now lots of things are going to be Comparable, but if you had a Vehicle class that's the basis of one tree (with Cra, Bicycle etc as child classes) then you might want that to be Comparable. But what if you also had an Animal class (with children like Dog, Cat etc) that you also wanted to be Comparable? Without interfaces you would force Vehicle and Animal to extend a Comparable base class.

    Now, what if you also wanted Animal classes to be able to doSomething()? DO you extend Comparable to add a doSomething() method? Then what happens if you also had a Building class that you wanted to doSomething, but not be Comparable?

    Put simply, interfaces provide you flexibility to define what a class can do. And what a class (and hence an Object) can do is the basis of OO programming.

    For your vehicle example you could stick with the basic set you define and also provide an EmergencyVehicle interface...say with a flashLights() and a soundSiren() method?
    well to me.. now it seems like an emergencyvehicle class is usless as it is extending vehicle.

    but a policeCAR and a fireTRUCK are both emergency vehicles but need to extend their respective vehicle type

    so yeah an emergencycehicle interface for all thing in common with emergency vehicles.. flashing lights, sires, the abilty to drive on wrong side of road and speed without getting in trouble


    oh and i guess say a Dog, cat rabbit could extend a skin or whatever its called interface..

    seeing as bird, dogs, cats, horses, frogs, are all animals but they all have different types of fur skin hair

    maybe they need interfaces while extending animal?
    Last edited by maknib; 11-05-2010 at 08:53 AM.

  4. #44
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    There's only a point in producing interfaces if there is some contract it makes sense to define. Think about what things do, not necessarily what attributes they have. That's what interfaces are all about...functionality.

  5. #45
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    oh you mean maybe something like.. cats n dogs can run and jump. but fish cannot run and jump, cats cannot fly, dogs bark, cats meow

    they all animals but they can't all do the same thing?

  6. #46
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    So you might, therefore, have an interface for SwimmingAnimal, with a swim() method...and an interface for a RunningAnimal with a run() method. (Interface names for example only...:))

    So if you had a game in which you had a list of Animals you could therefore have some code that handles moving sea animals that takes in only SwimmingAnimaland another for moving land animals that takes in RunningAnimal. The important thing there is that the code to process these does not care anything else about the object passed to it other than it being able to swim() or run().

  7. #47
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    so the Frog class would extend Animal and implement SwimmingAnimal

    and inside from i would define the Swim method that was declared in SwimmingAnimal ?

  8. #48
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Something along those lines.

    Obviously, outside of actual development, theoretical examples can be pushed too far, but you have the idea I think.

  9. #49
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    only took 3 pages to understand :P

  10. #50
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Think of it more as a day and a half...that's not too bad.

    You'll get a better idea as you do more complex apps.

  11. #51
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    well actually while you're here...

    Got any suggestion for a beginner app that i can to try to build?

    i wanna get my hand in there and get them dirty making a basic simple app for my first app

  12. #52
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    There's another (technical) reason: suppose a class A uses a class B and class B is changed and recompiled; you generally have to recompile class A as well. If, o.t.o.h. class B implements an interface I and A uses the interface, class A doesn't need to be recompiled when B changes (but the interface I stays the same).

    kind regards,

    Jos

  13. #53
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Quote Originally Posted by maknib View Post
    well actually while you're here...

    Got any suggestion for a beginner app that i can to try to build?

    i wanna get my hand in there and get them dirty making a basic simple app for my first app
    When learning a language I always used to come up with some noddy game or other (text based, normally). Or a character generator for an RPG (my geekiness knows no bounds). If you want to learn Java as a whole I'd aim at something that you can expand on. Something that eventually you can stick a db at the back and a Swing GUI at the front, but initially will maybe simply read things from a file and display on the command line.

    As for what exactly...no idea.
    :)

Page 3 of 3 FirstFirst 123

Similar Threads

  1. interface
    By tatya in forum New To Java
    Replies: 1
    Last Post: 06-19-2010, 06:22 AM
  2. Is there a GUI interface for...
    By smith427 in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •