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 there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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

  • 11-10-2007 1: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, Registered Users 2 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