Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Java, two questions: Interfaces and methods like method().method()

Options
  • 11-10-2007 2:19am
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    Hello

    I'm teaching myself Java at the moment. I have two questions if anyone wouldn't mind helping me?

    1. Am I right in thinking Interfaces are just a way of forcing people to code certain methods? In other words, you must implement the following methods x() and y()? If so, can you give me an example of when I'd want to use this rather than simply inheriting the methods?

    2. I've seen what I think were methods written like this: x().y(). I don't understand this. What exactly is going on there? I thought methods should be just written like methodName()? Is this two methods together? Also, things like System.out.println(). I understand this is a static method, but why isn't it just System.println()?

    Any help greatly appreciated.

    Thanks!


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    My Java's a little rusty, but I'll give it a go.

    An Interface provides a common framework for a set of objects which can be logically grouped, but where the high-level grouping isn't a specific object in itself.
    When a class implements an interface, it is telling the outside world that they can expect the methods of that interface to be available to them.

    OOP is always easiest to explain using real-world objects. Imagine a vehicle. A vehicle is a high-level construct. All vehicles have certain things in common - number of wheels, type of engine, etc. However, you can't use a "vehicle". You need to be more specific before you can use it, e.g. a car.

    So if you create a "Vehicle" interface class, and then use that class to create all the different child classes, e.g. car, truck, bike, train, then someone who comes along knows that the object inherits/implements type "Vehicle", so the getNumOfWheels() method will work, no matter what type of vehicle it actually is.

    To take a more realistic example, imagine that your program receives an array of different vehicle objects. Each object in the array could be a car, a truck, a bike or any other implementation of the "vehicle" interface. You know then that you don't even need to check what type of object each item in the array is - you have a common set of methods available to you.

    If the interface wasn't created, and you received an array of such objects, how would you know what method to call to get the number of wheels, number of passengers, type of engine, etc. Each object could have a range of different methods - you wouldn't have a clue what each one looked like.

    I hope that's explained well, it's a tough one to explain.
    I've seen what I think were methods written like this: x().y(). I don't understand this. What exactly is going on there? I thought methods should be just written like methodName()? Is this two methods together? Also, things like System.out.println(). I understand this is a static method, but why isn't it just System.println()?
    This is more shorthand than anything.

    If I write
    myObject.methodA().methodX();

    This is the same as writing
    Object temp = myObject.methodA();
    temp.methodX();

    Each method in the chain is evaluated in turn, starting left-to-right.

    In the case of System.out, System is just another class within the language. out is a pubic Object within that class (some sort of BufferedWriter IIRC).
    So when you call System.out, you are referring to the "out" object in the System class. When you call System.out.print(), you are calling the print() method, of the out object in the System class.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Thank you very much Seamus, your explanations make perfect sense.

    Much appreciated. :)


Advertisement