# Abstract extends with a body?

## Recommended Posts

I have a file called C.java inside of it contains..
abstract class A
{
abstract void foo();
}
abstract class B extends A
{
void foo() { int j = 0;}
}
class C extends B
{
}


How can this possibly compile? Abstract classes must have abstract methods (Unless the methods are static). foo() aint static yo. Why does this compile? Edit: Oops, I uploaded the wrong code, the one you see now is the more precise one. [razz]

##### Share on other sites
I don't know java, but translating to .net, I'd guess that since you defined the function in B, there are no abstract methods in B, but declaring it abstract prevents you from instantiating it.

Again, I don't know if java has restrictions here, but it seems inflexible to force you to have at least one non-static, abstract function. It would appear, if it compiles, that it doesn't have a restriction there. The abstract keyword appears to prevent you from instantiating the class and requires it to be extended.

##### Share on other sites
Well, there would be little point in using abstract classes at all if they couldn't define nothing but abstract methods, don't you think?

##### Share on other sites
http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html
Quote:
 An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses or implemented interfaces must be declared as an abstract class.

Arg... my course professor has us learn from a book that simplifies the complexites of OO in Java by telling us simplified statements that mean simply incorrectness! Inaddition, their definition of concrete classes is completly misleading!

##### Share on other sites
Quote:
 Original post by TheveninHow can this possibly compile? Abstract classes must have abstract methods

No, 'abstract' simply marks a class as 'incomplete' - it is not usable as-is, and the user needs to add additional implementation in order for it to be usable.

abstract class A {  abstract void foo();  abstract void bar();}abstract class B extends A {  void foo() { int j = 0;}}class C extends B {  void bar() { int b = 0; }}

Here you can clearly see that 'B' is incomplete (it lacks 'bar()') yet provides additional implementation over just 'A'. 'C' can still use the default foo() provided by 'B' or override it if it wishes.

In your original example, the 'abstract' on B is unnessisary, as theres nothing that is still missing.

##### Share on other sites
Quote:
 Original post by Anonymous PosterWell, there would be little point in using abstract classes at all if they couldn't define nothing but abstract methods, don't you think?

No, I don't; thats why we extend abstract classes.

##### Share on other sites
Quote:
 Original post by OrangyTangIn your original example, the 'abstract' on B is unnessisary, as theres nothing that is still missing.

Yes, and thanks for the example.
ratings++.

##### Share on other sites
Quote:
Original post by Thevenin
Quote:
 Original post by Anonymous PosterWell, there would be little point in using abstract classes at all if they couldn't define nothing but abstract methods, don't you think?

No, I don't; thats why we extend abstract classes.

If an abstract class is going to provide nothing but a list of abstract methods, you should be using an interface.

##### Share on other sites
Quote:
 Original post by Anonymous PosterWell, there would be little point in using abstract classes at all if they couldn't define nothing but abstract methods, don't you think?

THey could define interfaces!

Cheers
Chris

##### Share on other sites
Yup, but as OrangyTang said, you should be using interfaces in that case.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628307
• Total Posts
2981972

• 9
• 13
• 11
• 12
• 11