Abstract classes, Interfaces, and concrete classes


EJB design: Abstract classes, Interfaces, and concrete classes

  1. I have another technical interview tomorrow and somehow I keep getting asked the same question and I feel my answer is not really up to par. The question is:

    "What is the advantage of subclassing an abstract class versus concrete class?"

    "What is the difference of using an interface versus an abstract class, which is better to use?"

    For the first question, I usually answer performance is the advantage because you don't have to instantiate the class.

    For the second question, I usually say that you can put implementation in an abstract class and you can't in an interface. I really can't answer the second part to this question.

    Any ideas?
  2. From Design perspective,
    YOu really cannot say which is better, it is requirements driven,
    interface is a more generic class, which defines general features for all the objects implementing it,
    while an abstract class is almost the implementation, the classes inheriting will have an idea as what needs to be implemented

    for eg:

    public interface Vehicle{
        public abstract int capacity();
        public abstract int noOfCylinders();

    public abstract class SUV implements Vehicle{

    public abstract class Car implements Vehicle{
    public abstract String comfortLevel();
    protected int noOfWheels(){
    return 4;

    public class BMW extends Car{
         public String comfortLevel(){
    return "high";
  3. Abstract classes are "abstraction" of a class as the name suggests. As an example: The Human class can be an abstract class and in its concrete classes "man" and "woman", some of the functionalities are common. For example "human" can have a function "walks". Now "walks" is something that belongs to all humans. But "dances" is a function that might be different for "man" and "woman" or maybe to anyone who belongs to this class. So we over ride the "dances" functionality to provide seperate implementation to different implementors.
     Taking the same analogy further, any class that is abstract provides some basic functionality for that class. But more specific implementors have to over ride their specific functionalities... Hope u got the picture
  4. Abstract Classes and Interfaces[ Go to top ]

    You want to make a Abstract classes by default the abstract class should be public.Say if u r having 4 abstract methods and 2 concrete method that you are having in your abstract.If you want to utilize them you have to extend the class into some other subclass untill it should not be make use of it.

    class a extends b{


    Interface can be implements by some other class.
    class a implements [Name of the Interface]{


    Hope u got!!!!!!
  5. \Harrell\
    "What is the advantage of subclassing an abstract class versus concrete class?"

    In my opinion this question is kinda backwards - when you're subclassing, you usually don't have much control over whether the superclass is abstract or not :-) The real question should be "when should you use abstract classes vs. concrete classes" IMHO. The answer I give to that is you should use an abstract class when objects of that type should be be instantiable directly e.g. it's truly an abstract entity with no concrete existence, and you need a concrete subclass to make it "real".

    I've had that sort of question (in the original form you showed) on interviews, and in general when a question is formed awkwardly, feel free to redefine the question into something that makes sense to you, or come at it from a different direction than the intent of the original question.

    "What is the difference of using an interface versus an abstract class, which is better to use?"

    There's no "better" here :-)

    An abstract class has an advantage that you can attach some default behavior to it (e.g. an implementation). The downside is that any given object can only subclass _one_ class in Java, so using an abstract class exlusively for typing is severely limiting your users.

    On the flip side, classes can implement multiple interfaces, so you're giving the implementor more options. But - interfaces have no behavior attached to them (e.g. there's no default implementation). This means each person who implements the interface needs to implement the whole thing.

    For complex functionality, it's common to use both - first, an interface that defines a component's functionality, and then a base class that implements this interface in a default manner.

  6. I think I know you, unless this is a different 'Mike Spille'. Did you ever work in New York City for a company named Transaction Information Systems, say, 5 years ago?

  7. Katherine,
    I would like add my two cents:
    java.lang.Runnable & java.io.Serializable are my favorite examples.
    Try to work with them, you will feel more confident when understand them.
    Can some one suggest good abstract class example in JDK

    - Abdul
  8. TISNY[ Go to top ]

    Did you ever work in New York City for a company named Transaction Information Systems, say, 5 years ago?

    (Sorry for the off-topicness of this to everyone else).

    Yes, I worked for "TIS Equipment Corp" which rolled up into Transaction Information Systems which morphed into TIS Worldwide which again transmorgified into Starpoint Solutions, from July 1998 to March 2002. Can't recall anyone named "SAF" though :-)

  9. An old friend...[ Go to top ]


        Shoot me an email, I'd like to catch up with you. And don't worry, I've gotten over what happended at TIS when I worked in your group. Actually, it was a great career move, that's for sure :-D

    soundfactory_ at hotmail dot com

    SAF (Raffi)
  10. Also, you can get a flavor of multiple inheritance using interfaces/generalization.

    public class A extends B implements c{
  11. In some Open Source Software, I did see the following:

    public class A extends B,C,D{

    Is this allowed in Java?? Somehow the OSS seems to be compiling fine.
  12. Hi,

    I would suggest you read the GOF patterns book. It has many patterns where an abstract base class is used over a concrete class. A good example that I suggest you read is the Strategy pattern. The examples are in C++ but they are easy to follow.

    A good example of when an interface is used over a class is the proxy pattern.

    Good luck on the interview,