Reputation: 483
I am wondering if I have the correct understanding of the theoretical difference between a fully abstract class and an interface. I understand the technical differences.
My understanding is that classes are used for concrete objects and interfaces for features those objects can exhibit. So If I was making a car class, and I wanted that car to have a navigation feature, I would make a navigation interface rather than an abstract class correct? (Replace with any feature, i.e. automatic parking, etc)
Upvotes: 1
Views: 3515
Reputation: 4164
You are correct.
A boat or a plane could also have an automatic parking feature or a navigation system. But in most models, something is either a boat or a car (let's leave flying cars out...).
Practically, in Java an abstract class is a good way to force an extension in a particular (unique) category. If you have AbstractPlane and AbstractBoat, you are sure an object is either one or the other.
Upvotes: 2
Reputation: 28752
The theoretical difference between the two is off-topic here, you might want to ask at http://programmers.stackexchange.com.
The closest on-topic question for so is what you can do with them, which you probably already know -- a class
can implement
two interfaces
, it can extend
only one class
.
Upvotes: 1
Reputation: 4318
Yes, you are on the right track. Think of it this way. You have a fruit. But you don't know which one it might be. So you make an interface fruit which is upto the user now how may they use this interface. It might be apple, oranges or peach. The interface fruit will have a definite size, color and environment it grows. Though this information changes according to different types of fruits.
In other words, interface is like a skeleton of something very very specific that you are trying to accomplish in the long run.
Upvotes: 0
Reputation: 5662
Fully abstract class is very similar to interface, practically identical. When it comes to particular languages, differences arises. For example, in Java class can extend only one other class but can implement many interfaces.
Regarding your example is correct. Class is collection of 2 main things: data and methods to work with that data.
Upvotes: 0