Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Interfaces

Stayl T
Stayl T
968 Points

Difference between implementing and importing?

What is the purpose of using 'implements' on 'Comparable' and then using its compareTo() method? Can't we just import the class 'Comparable' and use the method as @override? We are using @override anyway!

1 Answer

I'm not sure if you already know this, but, Comparable is an interface, and not a Class.

You can both implement and import an interface. If you import an interface you don't have to implement it. But, if you implement an interface , you do have to import it if the interface is declared in a different package than the class that implements it.

Basically, what an import statement does, is it tells that file(package) what classes/interfaces are available to be used. But say, you had a main class that implements/imported the Comparable interface, if you were to make a class that extends your main class, but in a different package, that sub-class must also import the Comparable interface.

Let me know if this is confusing and I can try to re-explain.

Stayl T
Stayl T
968 Points

I didn't understand it completely. Can you explain with the help of an example? What is the major difference between an interface and a class?

Thanks.

Sure, here's a code example.

Bounceable.java

package com.somecompany.interfaces;

public interface Bounceable {
    int setBounceFactor(int bf); 
    void bounce();
}

Ball.java

package com.newpackage;

public class Ball implements Bounceable {
    public void bounce() {
        //ball specific 'bounce' code goes here.
    }

    public int setBounceFactor(5) {
        //ball specific 'setBounceFactor' code goes here
    }
}

SuperBouncyBall.java

package com.differentpackage;

import com.somecompany.interfaces.Bounceable;
import com.newpackage;

public class SuperBouncyBall extends Ball { //this is the sub-class of the Ball class
    public void bounce() {
        //super bouncy ball specific 'bounce' code goes here
    }

    public int setBounceFactor(15) {
        //super bouncy ball specific 'setBounceFactor' code goes here
    }
}

Since SuperBouncyBall is extending a class that is implementing an interface, the sub-class(SuperBouncyBall) MUST implement the Bounceable interface's methods.

To fully understand these code examples you need to know that in an interface, all of the methods are public and abstract. I don't wanna go into too much detail with abstract methods/classes, all you need to know is an interface IS ALWAYS abstract, and so are its methods. An abstract method has NO implementation, just the declaration of the method itself, and ANY concrete (non-abstract) class that implements an interface MUST implement all of that interfaces methods.

Technically, the SuperBouncyBall class doesn't need to implement the Bounceable interface's methods, since they have already been inherited by Ball.java.

In Ball.java I could have used the Bounceable interface two different ways:

package com.newpackage;

import com.somecompany.interfaces.Bounceable;

public class Ball implements Bounceable {
    public void bounce() {
        //ball specific 'bounce' code goes here.
    }

    public int setBounceFactor(5) {
        //ball specific 'setBounceFactor' code goes here
    }
}

or

package com.newpackage;

import com.somecompany.interfaces.Bounceable;

public class Ball {
    public void bounce() {
        //ball specific 'bounce' code goes here.
    }

    public int setBounceFactor(5) {
        //ball specific 'setBounceFactor' code goes here
    }
}
Stayl T
Stayl T
968 Points

Okay now things are getting clearer! Thanks a lot for your effort!

No problem, let me know if you have any other questions.