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

Regarding Java, what is the difference between an abstract and an interface?

I understand how abstracts are used to allow importing of methods that are not implicitly defined in a class. Once imported, abstract methods can me modified depending on the specific class or subclass. After learning about interfaces in class outside of Treehouse, I'm finding it difficult to distinguish them from abstracts. Thank you so much if you know how they differ and the implementations of each.

abstract classes still have the single inheritance issue where everything is a part of the same hierarchy. Interfaces allow unrelated classes to be treated as a single type based on having some common methods etc. Interfaces are often discussed in terms of a multiple inheritance work-around.

also maybe take a look at https://stackoverflow.com/questions/10040069/abstract-class-vs-interface-in-java . It's probably slightly older advice, but it will be what a lot of people have worked with. I understand interface has some new abilities these days (discussed at https://www.javaworld.com/article/2139921/learn-java/abstract-class-versus-interface-in-the-jdk-8-era.html ).

Personally, I'd use abstract when i really wanted the classes to be closely related.

2 Answers

If you don't want an object of a class to be instantiated or instantiation is unnecessary then you should use abstract class. for example, let's say you are building a game where animals are involved like Cat, Dog, Giraffe etc. you don't need a concrete animal object. you can abstract the Animal class and use it as a superclass of other concrete animal classes. because your scenario does not require an Animal object.

If you want to force a behaviour, you can use interfaces. Morevover, Java does not support multiple inheritance. By using interfaces you can kinda achieve this by implementing many interfaces

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface.