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!

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

Jiten Mistry
Jiten Mistry
4,698 Points

Whats the difference between abstract classes and interfaces? and when would we use each one?

Getting confused on the difference between these two, and when to use them. I understand subclass extends the abstract class, and subclass implements an interface. I know the interface contains on methods, and the abstract class contains variables and methods and abstract methods (these must be instantiated).

2 Answers

I think, interfaces are better used when there is a chance you could swap out one functionality with another. for example on a website you could use mysql as a database, then switch to MongoDB.. in which case you can write two classes (one for mysql, other for mongodb) that implement an interface so their methods will be same.. then you can easily swap one with other.

with abstract you can have some function's implementation coded and some can be abstract which your subclass has to implement..

as for when to use each, use whatever you think is best but be consistent throughout your projects.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey Jiten!

This is an age old question and newer versions of Java have made it even more confusing. The answer used to be easier, let me give explaining it a shot.

An abstract class cannot be initialized by itself, it must be extended to a non-abstract class. It may include code for certain methods, but its goal is to provide one more abstract methods that must be overwritten by the extender (class using created by extending the abstract class.) These are usually nouns by default, and can be thought of, make something really similar to this implementation, but you must override a these abstract method signatures. Since Java is a single inheritance language, there can only be one of these.

An interface on the other hand, just provides method signatures **. It does not include code. **Well, that is until Java 8 which introduced default methods. These most often are adjectives that end in able. I remember this as, the class should be able to do these required methods. Often this is the only thing that you really care about when using objects that implement an interface. As long as the object is able to XXX, or best stated is the object XXXAble, then accept it as a valid parameter. This makes interfaces nice and specific, but allow implementations to vary wildly. As long as they can be considered to be able to do something, they are acceptable. You can have many of these per class definition.

The line is definitely blurred with the new Java 8 default method, the answer used to be much more clear and you could state that interfaces contain no implementation details while abstract classes can. So now it's the number and type of statement they are trying to convey. As always it comes down to a style thing, they are similar and yes, will be confusing and sometimes misunderstood, which further makes things confusing.

Hope that helps! I hope to cover this in a Workshop sometime in the near future.