Sign in to follow this  
Thevenin

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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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!

"Head First Java", more like "Light Head Java" [flaming]

Share this post


Link to post
Share on other sites
Quote:
Original post by Thevenin
How 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.

Your example is slightly non-sensical though. Instead lets have:


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 this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
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?


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Thevenin
Quote:
Original post by Anonymous Poster
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
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?


THey could define interfaces!

Cheers
Chris

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this