Abstract extends with a body?

Started by
8 comments, last by GameDev.net 18 years, 4 months ago
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]
Advertisement
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.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
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?
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]
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.
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.

Quote:Original post by OrangyTang
In your original example, the 'abstract' on B is unnessisary, as theres nothing that is still missing.


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

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.
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
CheersChris
Yup, but as OrangyTang said, you should be using interfaces in that case.

This topic is closed to new replies.

Advertisement