1

So abstract classes can be extended, but so can any class right? So why are some specific classes abstract and others not?

Furthermore, I have tried to figure out but I always get rather confused but what is the actual difference between an interface and an abstract class?

Thanks to anyone who can clear this up for me!

Tim
  • 1,056
  • 4
  • 17
  • 34
  • 4
    This is a basic question on language concepts. You'd best be served by reading a book or [browsing wikipedia](http://en.wikipedia.org/wiki/Abstract_type). – Kirk Woll Apr 10 '12 at 22:42
  • 1
    The second question is answered [here](http://stackoverflow.com/a/681638/13531). – Michael Myers Apr 10 '12 at 22:42
  • 2
    Your questions have been discussed to death here and elsewhere. Better to ask more specific points of misunderstanding. Just what about the existing discussions confuse you? – Hovercraft Full Of Eels Apr 10 '12 at 22:45
  • @HovercraftFullOfEels Main question is: If you can extend any class, what is the point of abstract classes? There is no difference extending a class or an abstract class, is there? – Tim Apr 10 '12 at 22:46
  • An abstract class can declare method signatures without implementing them. It allows to invoke them but the extending class has to implement them. – Guillaume Polet Apr 10 '12 at 22:47
  • 2
    @Tim: some classes should not be instantiated. For instance consider an Animal class. In some contexts it makes no sense to create an Animal object, and much more sense to create an instance of a *specific* child class, such as a Tiger instance. – Hovercraft Full Of Eels Apr 10 '12 at 22:49
  • 1
    @Tim As an example, you could have an abstract class Human which declares an abstract method public abstract String getGender(). Then you could have a concrete class Male and Female which implements differently the getGender() method. In your Human class you could have a toString() method invoking that getGender() method without actually knowing in advance what is the body of it. – Guillaume Polet Apr 10 '12 at 22:52
  • 2
    For reference, and for completeness's sake...you can't just "extend any class" in Java. If a class is declared `final`, the compiler won't let you extend it. – cHao Apr 10 '12 at 22:55
  • When you are told to build a Missile what's your fist step ? The first step to build something which is not exactly known is ABSTRACT it out. So for Missile you will think 1- lets have some explosive component inside it. At this moment you know for sure that missile must have explosive contents but you dont know what it is until you come to know (a very later point of time) that i am suppose to build a submarine-launched ballistic missile and is armed with nuclear warheads and is launched from nuclear-powered ballistic missile submarines (SSBNs) etc.. that time u have concrete class. – Dhananjay Apr 11 '12 at 05:03

3 Answers3

7

An interface can only describe method signatures, eg

public void add(int a, int b);

And a class that implements it, must define the method.

An abstract class can only be inherited, but it can still define a method:

public void add(int a, int b) {
    return a+b;
}

And any classes that implement it can use that method. Abstract classes can also implement final methods, so they can never be re implemented by a class that extends it.

dann.dev
  • 2,406
  • 3
  • 20
  • 32
  • so final methods cannot be overridden? When an abstract class is inherited, must it also define all non-implemented methods? Thanks. – Tim Apr 10 '12 at 22:54
  • 2
    @Tim Yes (that's the point of the final keyword) and yes, or it must be abstract as well. – Niko Apr 10 '12 at 22:57
5

The usefulness of an abstract class is to factor functionality (both methods and attributes) common to several classes. Also, it's not correct to say that any class can be extended, final classes can not be extended.

An abstract class is different from an interface because an interface only defines a functional contract: what needs to be accomplished, but never indicates how to implement it, whereas an abstract class can flesh out some general, abstract implementation details but leave the concrete aspects open to be implemented by its subclasses. In a way, an abstract class is like the middle point between an interface and a concrete class.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
5

The point of abstract classes is meaningful default behavior.

Abstract classes can provide meaningful default implementations in those methods that make sense and leave only abstract methods for clients to implement. Clients may choose to override those default implementations if they wish.

Interfaces provide no such opportunity; they're all abstract methods that must be implemented by clients.

It's not an either/or proposition; often both are provided. See the java.util.Map and java.util.AbstractMap, for example.

duffymo
  • 305,152
  • 44
  • 369
  • 561