7

Does the class need to have the abstract keyword before it? Or Does it need to have unimplemented (abstract) methods? Can any normal class be extended?

2

7 Answers 7

5

Yes, all methods which are not final (static is also a bit different form the rest), can be overridden, unless the class itself is declared final. Abstract methods are only used if you do not provide any implementation in the base class.

4

Can any normal class be extended?

Yes :) Unless it has the final modifier.

0
3

No it doesn't need to have the word abstract, the word abstract just wont allow you to create an instance of that class directly, if you use the word abstract you can only create an instance of the classes that extend that abstract class.

AbstractClass abs = new ChildClass();
1

Yes you can extend a class without it needing to be defined as abstract. What this means is that you will be overriding methods. For example, you might make a class

DifferentString extends String

then,

public String toString()
{
    return "Something different";
}

This will mean you can change the original behaviour of the parent class.

Reference:

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

1

As others have also said, yes - but it is good practice to avoid doing so if you can possibly help it, because you can end up with what is known as the fragile base class problem more easily.

4
  • So fragile base class is basically, changes in a superclass method resulting in unexpected behaviour in the subclass. But is it possible, if the same method is overridden in the subclass?
    – Nitin Garg
    Mar 6, 2012 at 14:06
  • 1
    yes, if that overridden method depends on any other method in the superclass. A simple change to the superclass can result in unexpected behaviour in the subclass, even if the superclass still fulfils its contract.
    – Robert
    Mar 6, 2012 at 14:33
  • From the Wikipedia page, I get it that using Interfaces solves this problem. But, how does using the abstract keyword prevent this problem? (or, how does not using the abstract keyword cause it?) Sorry, I don't understand completely.
    – Nitin Garg
    Mar 6, 2012 at 14:38
  • it doesn't. What is important is to avoid extending concrete classes where possible, that way you can minimize the chances of FBC. Abstract classes provide a halfway point, where you can implement high level algorithms delegating specifics to its subclasses. See Template Method design pattern, for instance.
    – Robert
    Mar 6, 2012 at 18:30
1

Yes, but if the class is marked as final, it can't be extended.

class Foo {
    //some code here
}

class Boo extends Foo {

}
0

There are some concept confusions here. Unimplemented methods reside in an interface, and you can implement that interface.

You can extend any class as long as its not final.

Edit: What I meant to say that it is preferable to put Unimplemented methods in interfaces as well. Sorry for the poor wording. Abstract classes can have unimplemented methods as well, though you will end up with a complex and rigid hierarchy. .

3
  • Am I correct to say that an abstract class MUST have at least one unimplemented method?
    – Nitin Garg
    Mar 6, 2012 at 14:09
  • No. It may or may not have methods.
    – n_x_l
    Mar 6, 2012 at 14:26
  • @why-el What I meant to say that it is preferable to put Unimplemented methods in interfaces as well - not sure I agree. Interfaces and abstract methods/classes serve different purposes. Interfaces are about decoupling, abstract classes are about extensibility and common base-functionality. The one neither implies nor requires the other. Putting abstract methods in interfaces as well can be confusing as it implies that the method is part of a contract rather than, more commonly, something expected to be implemented to complete the functionality of an abstract base class.
    – pap
    Mar 6, 2012 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.