• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstraction and interface  RSS feed

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, i'm new to Java and was reading about the fundamentals of Java and I keep coming across two things that I can't seem to understand there meaning and how they are used. Abstraction and interface. Can anyone give me a simple and straight forward meaning and instances where you would use abstraction over interface and vice versa
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lines between an Abstract class and an Interface are starting to blur a bit in Java 8, but basically, an Interface is a contract specifying the behaviour of a class. So if you want to guarantee that a class has the ability to bark, you could create an Interface:

Now if you want to communicate that a class has the ability to bark, you implement Barkable:

The really nice thing is that if you need a class that can bark -- any class -- you can specify just that it need to be Barkable:

The animal can be a dog or wolf or poodle or whatever, as long as it implements Barkable. And you know you can call the method bark() an animal.

So what's an Abstract class? It is a class that cannot be instantiated (created with new) that may (and usually does) have abstract (unimplemented) methods. You have to extend an Abstract class, as opposed to implement an Interface. When you extend (subclass) an Abstract class, you then implement any abstract methods. Like an Interface, you can guarantee certain behaviors. Unlike an Interface (until Java 8) you can have default implementations of methods. An Abstract class is usually used as a superclass of a group of subclasses.

Now let's look at the Dog class:

In general, if a class extends another class, it has an IS-A relationship, that is, a Dog IS-A Animal. You can also use an Abstract class as a type, just like an Interface:

So an interface guarantees behaviour, an Abstract class creates an IS-A relationship. It's a subtle difference. Also, a class can implement more than one Interface, but can extends only one Class.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The semantics of the "-able" suffix are somewhat different from what "Barkable" actually conveys to me.

If you're talking about barking or making some kind of noise, I'd probably go with something like "Audible" instead to make the interface name fit the semantics better.

Have a cow for your explanation anyway
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you have a class, say, Car, then if it implements Barkable :

then you can also say thing like


So you see that ANY class that implements Barkable works. That would be different than an abstract class named Animal which had an abstract method "bark()", in which case only subclasses of Animal could use that bark method.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mitchell bat wrote:Can anyone give me a simple and straight forward meaning and instances where you would use abstraction over interface and vice versa

I assume by "abstraction" you mean creating an abstract class.

My take:
1. If you're not sure, favour interfaces.
2. If you have something that you can provide a general implementation for (whole or partial), write an abstract class.

And you often see the two used together. For example: A List in Java is a predictably ordered collection of items that can be directly accessed individually.

Sounds pretty “abstract” right? And yet it's the basis for practically every list (other than an array) that you use in Java. And the List interface defines all the methods that a "list" is required to provide (and there are quite a few).

What does 'predictably ordered' mean? Well, basically it means that once an item is in a list, it must retain its position relative to others unless
(a) Another item is inserted before or after it.
(b) It's removed.
So if you go through a List more than once, its items will be delivered in the same order every time unless something is changed.

OK, so that's a List. What about implementing it?

Well, one thing that a List is required to do is return an Iterator so, if you assume that someone else has told it how to return a specific item (see List.get(int)), you can implement an Iterator that successively returns items numbered from 0 (the first item) to the last one in the list.

In fact, AbstractList (←click), which is an abstract class, does far more than that. It's an “almost complete” implementation of a List that only requires you to add four methods - get(), set(), size() and remove() (and not always all of those) – to implement a concrete List class based on any structure you like.

Pretty powerful, eh?

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!