Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+33 votes

Can someone describe the difference between abstract classes and interfaces?

asked in CSC285_Fall2018 by (8 points)

2 Answers

+14 votes

Abstract classes can have non-abstract methods that subclasses can inherit and use. Interfaces contain only method headers that a class that implements the interface MUST have.

answered by (8 points)
+6

Well, technically, Java 8 introduced "default methods" (see: https://dzone.com/articles/interface-default-methods-java), so interfaces can have some actual code in them, but it's much more restricted than abstract classes. Interfaces still can't have non-final data fields, but abstract classes can.

+12 votes

Both of them can be used for reused code. Abstract class can have field, .. and others things inside, which interface can't have. Moreover, You only can entends one class, but as many as interface you want.

answered by (8 points)
+6

Interfaces aren't as good for code re-use (although there is some possibility for that, thanks to the "default methods" added in Java 8). Interfaces are mainly helpful for supporting polymorphism.

...