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?

8
  • 2
    possiblity duplicate stackoverflow.com/q/1320745/668970 Commented May 13, 2011 at 11:11
  • 2
    I don't think so, Damodar. This is about the need for abstract, not about the meaning of the concept. Commented May 13, 2011 at 11:14
  • I agree with Urs Reupke. Commented May 13, 2011 at 11:15
  • please check the question once agian "More General, when will we decide that a class is an abstract class? " Commented May 13, 2011 at 11:15
  • 5
    It's pretty easy to answer the first question by observing that java.lang.Object isn't abstract... Commented May 13, 2011 at 11:16

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Thank you for the comment. Does a class that doesn't have a declaration as abstract can contain an abstract inner methods?
No class bneeds to be declared as abstract for this case
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.

Comments

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".

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.