1

Possible Duplicate:
Abstract class in Java

In Java,

Does every class that other classes extends from must be an abstract class?

and if no, under what conditions?

More General, when will we decide that a class is an abstract class?

Community
  • 1
  • 1
Unknown user
  • 44,551
  • 16
  • 38
  • 42

6 Answers6

6

Absolutely not. Classes can be extended without being abstract.

Usually you make a class abstract when you need to define the behaviour of some method based on other methods, but you cannot provide the implementation of those methods in this class.

For example:

public abstract class Vehicle {
   public void goHome() {
       move(getHome());
   }
   public abstract void move();
}

class Car extends Vehicle {
   @Override public void move() { //use engine };
}
class Bicycle extends Vehicle {
   @Override public void move() { //use pedals };
}

(a class cannot have abstract methods if the class itself is not abstract).

Another valid reason is that the class is some base class and it does not make sense to be instantiated. You can specify a protected constructor or you can make it abstract.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
3

Does every class that other classes extends from must be an abstract class?

No

if a class contains abstract in its declaration then its considered as abstract

For example:

public abstract class Foo{}

Read More

jmj
  • 237,923
  • 42
  • 401
  • 438
1

Every classes super class is not an abstract class.

For example Object class is the super class of all classes but it is not abstract.

We will use abstract class where we don't want the programmer or user to create an object.

And also to bind default properties and behaviour so that class extending the abstract has to compulsory inherit that properties.

Ankit
  • 2,753
  • 1
  • 19
  • 26
1

Every super class need not be abstract, but Scott Meyer argues in his "Effective C++" that every non-leaf class in an object hierarchy should be abstract. Now that we have Java, that concept should probably be extended to read "abstract or interface".

duffymo
  • 305,152
  • 44
  • 369
  • 561
1

You can see an abstract class as some kind of a prototype version of a product. For instance, you have a specification for a product and there are a couple of ways they may be implemented. An abstract class specifies all those specifications, however it doesn't give any implementation since there is no default, they have to be implemented differently by all kinds of versions of the product (your subclasses). It may however already implement the features all kinds of the product will have.

This automatically means you can't initialize an abstract class, since it doesn't implement some parts of the specification.

Joost
  • 10,333
  • 4
  • 55
  • 61
0

To answer your general question, we probably want classes to be abstract to have some general behaviour in it and subclasses to have specific implementations.

I have seen it mostly at a lot of places where its good to have some default generic behaviour which only a few subclasses need to change.

Also, as statd in another answer, if we dont want the user to instantiate the general class but want to force him to use a specific implementation, abstract helps.

Kichu
  • 667
  • 1
  • 6
  • 9