0

Learning a little-bit about java Interfaces and Implements made me question ,

  1. if Inheritance works only with classes does the Implements work only with Interfaces , or it may has another uses?

  2. Does java has to offer us "built-in" Interfaces that we can Implements into our program without creating it? and if so where i can find a list of those.

homerun
  • 19,837
  • 15
  • 45
  • 70
  • 1
    1. Yes, you can only implement interfaces. 2. http://docs.oracle.com/javase/7/docs/api/ – JB Nizet Feb 01 '15 at 15:30
  • There are literally _hundreds_ of `interface`s you can implement in Java for all sorts of reasons. But yes, you `extends` a class and `implements` an `interface`. Unless you are an `interface`, then you `extends` an `interface`. – Boris the Spider Feb 01 '15 at 15:31
  • class might `extends` another class and `implements` interfaces, yes implements is used only for implementing interfaces. By build in interfaces, do you mean one from java api? – user902383 Feb 01 '15 at 15:31
  • Recommended reading http://docs.oracle.com/javase/tutorial/java/concepts/interface.html – Doon Feb 01 '15 at 15:32

2 Answers2

1

if Inheritance works only with classes does the Implements work only with Interfaces , or it may has another uses?

Inheritance in general can apply to classes and interfaces through the use of the extends keyword. That is to say, a class can inherit properties and functions from another class, whereas an interface can expand its contractual obligations from another interface.

Example:

public interface Phone {
    String getNumber();
}

public interface MobilePhone extends Phone {
    public boolean isSmartPhone();
}

If one were to implement the MobilePhone interface above, they'd also have to implement the getNumber() method as well.

As for implements - that only works with interfaces.

Does java has to offer us "built-in" Interfaces that we can Implements into our program without creating it? and if so where i can find a list of those.

They're not "built-in" per se; they're prewritten for you in Java. The Java API is the best place to look for first-party interfaces; however, it's worth noting that you can get third-party interfaces from other packages and frameworks, such as Spring, Guice, and Guava, or from some other developer's JAR that you happen to include in your project.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • with that being said , i got another question if you may.. 1. does interfaces have purpose if i work on a project myself? 2. why the `Implements` exist if the `Extends` can do the same and more? – homerun Feb 01 '15 at 18:34
  • 1
    1. There may be times in which you want to create an abstraction but implement it in a few different ways to suit performance or testing needs. 2. `extends` is conceptually different than `implements`; `implements` enforces a compile-time contractual obligation with its implementor. [This reference](http://docs.oracle.com/javase/tutorial/java/IandI/index.html) should help with understanding the differences. – Makoto Feb 01 '15 at 20:45
0

Only interfaces can be implemented.

JAVA have many builtin interface that you can implement based on your requirement.

More than that a class can implement only interface while it can extend more than one class

Dipen Adroja
  • 415
  • 5
  • 15