Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use an abstract Class/Method, and when to use an interface  RSS feed

 
Richard Attix
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JavaRanch folks!  I need some guidance!  I'm trying to gain a solid understanding of abstract classes/methods and interfaces at the moment.  I've read info from a few sources, and watched a few videos, and I feel
that I can certainly write code using abstract classes and using interfaces at this point.  I'm still going to do some more repetitions on it, but I think I get it.  What isn't so totally obvious to me yet is exactly when you would use
one over the other.  I realize that the methods in interfaces are abstract methods and all.....I guess I'm just trying to totally get a grip on where each one works best.

Could someone try to give it a shot explaining this to me?  I would really appreciate the help. 

Thanks!

Richard the noob!
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Functional Difference

There is not much of a functional difference between abstract classes and interfaces anymore. It used to be the case that abstract classes could choose to define a few methods, and leave others abstract to be defined, but in the current java version interfaces also have this functionality (methods can be declared default to provide an implementation). However there are still a couple important differences:
- Abstract classes can have instance variables. Interfaces cannot.
- You can implement several interfaces, but you can only extend one abstract class.
- All methods in an interface are public. Abstract classes can have different scope.
- Not many people use default interface methods. If you have to define methods, a class may be a better choice.

Nonfunctional Difference

There's also a difference in how abstract classes and methods are supposed to be used.

Implementing an interface usually describe a behavior that the class can perform. For example, Java's interface Serializable. This clearly tells you what it's going to do: anything that implements this can serialize.
Extending an abstract class (or any class) shows an "is a" relationship. For example, when a class Dog extends a class Animal, we could say "A dog is an Animal". If extending a class is the correct choice, you should usually be able to put a sentence like that together using "is a" and have it make sense.


As for where each one works best: that is a design decision you have to make, depending on how much detail you want to give to the class that extends or implements it, as well as which type makes more sense (is this a behavior, or is it an is-a relationship).
An interface describes what is going to happen and provides rules for what happens, but does not specify anything about how to do so.
Abstract classes may have implementation details in them.
 
Campbell Ritchie
Marshal
Posts: 56187
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you seen our FAQ?

Unfortunately the definition of interfaces changed about 3½ years ago, by allowing static methods and default methods. Have a look at the Stream interface, which has both kinds of method, for examples.
 
Richard Attix
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary, thank you for the in-depth answer to my question.  It was very helpful and solved my problems.  Much appreciated!

Campbell, thanks for the extra added info.  It was also very useful!

Gotta love CodeRanch!!!  The best place to get help from the best minds! 

Thanks to all....

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