Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
you can use non-abstract methods in interfaces too :) Since java7 I think. But it's rarely a good idea to do that.
Interfaces are there do define a type of the class/object and define what it can do.
Abstract classes on top of that define some default behaviour
Interfaces: all the contents are static.
Abstract classes: not necessarily (i.e. you can have instance-speciffic state)
In Java you can implement multiple interfaces, but you can only extend one class.
Interfaces can be proxied and loaded dynamically w/ ServiceLoader easily. It's not the case for Abstract classes.
Usually abstract classes are used when you have the base behaviour defined in your class but you still need a few things for that clockwork to run. It's an "almost working mechanism". Derivates of an abstract class supply those missing implementation-speciffic properties/implementations.
Interfaces are usually used to define an abstract set of properties of the class: type, methods' signatures. That's all. -
@ahtolba Think of it this way (if you are familiar with SOAP):
interface is a WSDL file
abstract class is a set of stubs generated from that wsdl file: they know what structures to send, what responses to receive, how to map/process them, but they still require you to implement a few methods, like getConnection() or so.
Related Rants
So it's a basics question I know, but is the main difference between abstract classes and interfaces is just that you can use non-abstract methods in abstract classes but not in interfaces ??
question
interfaces
abstract classes
oop