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

Cannot understand these keywords

So I recently took the Unit Testing in Java course. In it, Craig used quite a few keywords which were very confusing to say the least. They were - interface, implements, abstract, and extends. Here are my questions :-

  1. What is the difference between implements and extends? Are they basically the same thing? And what do they mean?
  2. What is the meaning of interface and abstract? Where can/should I use them? And how many ways can I implement them? For example :- I can write interface in 2 ways -

Like this:

public interface someClassName {

}

And like this:

public class someClassName interface List {

}

That would mean that I can implement interface in 2 ways.

Hope you can understand what I'm trying to say. Any answer is appreciated!

Thank you!

  1. Implements refers to interfaces (you implement an interface). Extends refers to classes (you extend a class). Otherwise, the keywords are pretty similar. There are others differences (a class may only extend one class, but may implement multiple interfaces, for example).

  2. An interface specifies a set of method signatures, but it contains no implementation. A class contains the implementation of those methods along with their signatures. The abstract keyword on a method declaration allows you to omit the implementation (similar to a method declared on an interface). The abstract keyword on a class declaration indicates that the class cannot be instantiated directly (it must be extended to provide the implementation of any abstract methods).

An interface then is similar to an abstract class with no members (state) and only abstract methods. An additional difference is that a single class may extend only one class, but may implement multiple interfaces.

The examples you gave are doing two different things.

This one is declaring an interface. You would generally provide method signatures that would be implemented by another class. public interface someInterfaceName {}

This one (assuming you meant "implements" and not "interface") is implementing and interface. The class bekiw would provide the implementation (body) for any methods declared in the interface. public class someClassName implements someInterfaceName {}

On a side note - while we often use the word "implement" as a synonym for writing code, it has a more specific meaning with respect to interfaces. So, implementing the interface in this context means writing a class that provides the body for the interface methods. Writing the interface itself is "declaring".

2 Answers

Hi

Here is a nice discussion about interfaces and it will help answers some of the questions you have.

http://beginnersbook.com/2013/05/java-interface/

Hope this helps.

Thanks

Thank you so much! :D

Hello

Let try with a simple example

public interface someInterfaceName { public boolean doYouAgreeWithMe(); }

Note: I am naming it someInterfaceName (as opposed to someClassName as you did, for clarity)

Note: I added a method header with no implementation ... e.g.; the method has no body

now let say I want to make use of this interface

public class someMyClassName implements someInterfaceName {

//because I am implementing someInterfaceName , I must provide my own implementation of doYouAgreeWithMe

public boolean doYouAgreeWithMe(){

  String myFeeling = "Good"
  if(myFeeling.equals("Good"){
     return true;
  else return false;

} }

Now you come alone and you want to make use of this interface in your class

public class someYouClassName implements someInterfaceName {

//because you are implementing someInterfaceName , You must provide your own implementation of doYouAgreeWithMe

public boolean doYouAgreeWithMe(){

  String StockMarketToday= "high"
  if(StockMarketToday.equals("high"){
     return true;
  else 
     return false;

} }

do you see how same interface is implemented in 2 different classes, each has own implementation?

The beauty though is that in large, complex project, you need to hide the implementations ... by using interfaces, you are truly hiding the implementation ... and hence, you can later on do this

public class someYouClassName implements someInterfaceName {

//because you are implementing someInterfaceName , You must provide your own implementation of doYouAgreeWithMe

public boolean doYouAgreeWithMe(){

  String isItChristmas= "yes"
  if(isItChristmas.equals("yes"){
     return true;
  else 
     return false;

} }

without impacting anyone using your function.

Hope this helps. If this answers your question, please mark the question as answered.

That is a great explanation! But instead of making an interface and defining an implementation of that interface by ourselves, isn't it more time-saving by just creating a new method by the name of doYouAgreeWithMe(). That would instead, save a lot more time.

Or one way I can see myself using this would be if I wanted to define an object and have some set properties for that object and anyone who would implement that object into their class, would need to define those preset functions.

I can also see myself using this to maintain clarity in a program. But I could still just go for a method(which saves more time) over an interface which would take me a little more time to make.