www.digitalmars.com         C & C++   DMDScript  

D - abstract problems

reply "DeadCow" <deadcow-remove-this free.fr> writes:
Hi,

Im having trouble understand how the abstract modificator works in D ( there
is no discution about it in the documentation ).

This code :

abstract class A() {
    public void foo() {}
}


fail to compile with the following message :

"function foo abstract functions cannot have bodies"

!! kind of strange isn't ? First it's not a function it's a method. Second,
it's not abstract. I just want an abstract class with an already implemented
method. Where I am wrong ?

Thanks

-- Nicolas Repiquet
Aug 20 2003
parent reply "Digital Mars" <tboon gte.net> writes:
Change the code to

abstract class A
{
    void foo();
}

Since the class is declared abstract, the method is abstract as well.
Abstract methods cannot have a body (that's why they're abstract), hence the
compile error. The error message is not strictly correct, but does describe
the problem...

"DeadCow" <deadcow-remove-this free.fr> wrote in message
news:bi059h$2hlb$1 digitaldaemon.com...
 Hi,

 Im having trouble understand how the abstract modificator works in D (
there
 is no discution about it in the documentation ).

 This code :

 abstract class A() {
     public void foo() {}
 }


 fail to compile with the following message :

 "function foo abstract functions cannot have bodies"

 !! kind of strange isn't ? First it's not a function it's a method.
Second,
 it's not abstract. I just want an abstract class with an already
implemented
 method. Where I am wrong ?

 Thanks

 -- Nicolas Repiquet
Aug 20 2003
parent reply "DeadCow" <deadcow-remove-this free.fr> writes:
Hi,

"Digital Mars" <tboon gte.net> a écrit dans le message news:
bi19ei$1472$1 digitaldaemon.com...
 Change the code to

 abstract class A
 {
     void foo();
 }

 Since the class is declared abstract, the method is abstract as well.
Isn't the opposite ? : "If a method is declared abstract, the class become abstract."
 Abstract methods cannot have a body (that's why they're abstract), hence
the
 compile error. The error message is not strictly correct, but does
describe
 the problem...
So what the difference between an abstract class and an interface if i cant implement some methods ? Example, im trying to implement the visitor pattern on a node hierarchy. Here my base node class: abstract class Node { void accept( Visitor visitor ) { visitor.visit( this ); } } This dont want to compiles ( it does in java ). How can i code this ? Remove the keyword abstract on my class declaration ? But this class *is* abstract, its not a concrete node =) Im lost. Thanks -- Nicolas Repiquet
Aug 21 2003
parent reply "Digital Mars" <tboon gte.net> writes:
You can have abstract classes in (at least) two ways. Either declare the
entire class abstract, which means that there are no implementations in the
class, or you can declare a method abstract, in which case the entire class
is still abstract (as you pointed out below), but the class can still have
implemented methods. So, fo instance:

abstract class A
{
    void one()        //error: cannot have a method body in an abstract
class
    {
    }

    void two();       // OK, abstract in this case means no implementations
}

class B
{
    abstract void one()
    {                        // Error: method declared as abstract
    }
    abstract void two();    // OK, abstract means no implementation
    abstract void three()    // OK as well, can have an implementation - the
class is not declared abstract
    {                                    // even though it is due to other
abstract methods
    }
}

The difference between a class declared as abstract and an interface is that
a class declared as abstract is still a class; i.e. it can still have data
members, whereas an interface cannot. Also, you can inherit from only one
class, but many interfaces. For instance:
abstract class A
{
    void one();
    int data;
}
interface B
{
    void two();
}
interface C
{
    void three();
}
class D : A, B, C
{
    void one() {data = 1;}
    void two() {data = 2;}
    void three() {data = 3;}
}

What I would suggest for your Node example is something like:

class Node
{
    void accept( Visitor visitor )
    {
        visitor.visit( this );
    }

    abstract void doSomething();
}

class concreteNode : Node
{
    void doSomething()
    {
        //functionality here
    }
}

This would give the behavior that you seem to require.

BTW, figuring this out showed how cool and pwoerful D really is!

Regards,
Taylor Boon

"DeadCow" <deadcow-remove-this free.fr> wrote in message
news:bi23q9$2c0o$1 digitaldaemon.com...
 Hi,

 "Digital Mars" <tboon gte.net> a écrit dans le message news:
 bi19ei$1472$1 digitaldaemon.com...
 Change the code to

 abstract class A
 {
     void foo();
 }

 Since the class is declared abstract, the method is abstract as well.
Isn't the opposite ? : "If a method is declared abstract, the class become abstract."
 Abstract methods cannot have a body (that's why they're abstract), hence
the
 compile error. The error message is not strictly correct, but does
describe
 the problem...
So what the difference between an abstract class and an interface if i
cant
 implement some methods ?

 Example, im trying to implement the visitor pattern on a node hierarchy.
 Here my base node class:

 abstract class Node {
     void accept( Visitor visitor ) {
         visitor.visit( this );
     }
 }

 This dont want to compiles ( it does in java ). How can i code this ?
Remove
 the keyword abstract on my class declaration ? But this class *is*
abstract,
 its not a concrete node =) Im lost.

 Thanks

 -- Nicolas Repiquet
Aug 21 2003
next sibling parent reply "DeadCow" <deadcow-remove-this free.fr> writes:
"Digital Mars" <tboon gte.net> a écrit dans le message news:
bi368j$t64$1 digitaldaemon.com...
 You can have abstract classes in (at least) two ways. Either declare the
 entire class abstract, which means that there are no implementations in
the
 class
Here is the point. Why an abstract class *must* have all its methods abstract too ? It doesn't make sens for me ! [snip]
 What I would suggest for your Node example is something like:

 class Node
 {
     void accept( Visitor visitor )
     {
         visitor.visit( this );
     }

     abstract void doSomething();
 }
Hum ... i have no need for a doSomething method =) BTW, what the problem with making abstract class having implemented methods ( is there a technical problem ) ? Java allow this and it's very handy.
 BTW, figuring this out showed how cool and pwoerful D really is!
Sorry i dont see the real benefit of this over the java implementation of the abstract keyword but i agree that D is really cool and powerful. In java ( you probably already know that ) a class *must* be abstract if at least one of its method is abstract, but *can* be abstract even if no one of its method is abstract. So you can do all D do, plus my Node class without "doSomething" added =). Regards -- Nicolas Repiquet
Aug 21 2003
parent reply "Philippe Mori" <philippe_mori hotmail.com> writes:
 Here is the point. Why an abstract class *must* have all its methods
 abstract too ? It doesn't make sens for me !

 [snip]

 What I would suggest for your Node example is something like:

 class Node
 {
     void accept( Visitor visitor )
     {
         visitor.visit( this );
     }

     abstract void doSomething();
 }
 In java ( you probably already know that ) a class *must* be abstract if
at
 least one of its method is abstract, but *can* be abstract even if no one
of
 its method is abstract. So you can do all D do, plus my Node class without
 "doSomething" added =).

 Regards

 -- Nicolas Repiquet
So you want that derived class always override all the methods of the abstract class even if they are not modified (i.e all methods are abstract but may be implemented).? In either case, I do not really see the purpose of abstrat for the class itself... Should it prevent methods from being implemented (force an interface but allows data members) or should it makes all methos abstract... Both cases seems useless for me...
Aug 21 2003
parent reply "DeadCow" <deadcow-remove-this free.fr> writes:
"Philippe Mori" <philippe_mori hotmail.com> a écrit dans le message news:
bi423m$27ff$1 digitaldaemon.com...

 So you want that derived class always override all the methods of the
 abstract
 class even if they are not modified (i.e all methods are abstract but may
be
 implemented).?
Of course not. Here is my point of view: - an abstract method is a "not implemented" method. - an abstract class is a "not concrete" class. Something you can derive from but that you cant instanciate. - a class with an abstract method become abstract too. - there is no reason for a method to be abstract because its class is abstract.
 In either case, I do not really see the purpose of abstrat for the class
 itself...
It means "This is a base class, you can derive it, but you cant use it directly": abstract class Shape { int x = 0,y = 0; void setPos( int x, int y ) { this.x = x; this.y = y; } } class Circle : Shape { } class Rectangle : Shape { } Shape is not a concrete object, its just a "concept", coordinates are a part of this concept. Shape s = new Shape(); // it doesn't make sens. Shape s = new Circle(); // that make sens. s.setPos( 10 , 10 ); // it make sens too.
 Should it prevent methods from being implemented (force an interface but
 allows data members) or should it makes all methos abstract... Both cases
 seems useless for me...
It simply make the class itself abstract. Sorry I have some difficulties to explain it because I dont speak english very well. -- Nicolas Repiquet
Aug 21 2003
next sibling parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"DeadCow" <deadcow-remove-this free.fr> wrote in message
news:bi46nj$2ehh$1 digitaldaemon.com...
 Here is my point of view:

 - an abstract method is a "not implemented" method.
 - an abstract class is a "not concrete" class. Something you can derive
from
 but that you cant instanciate.
 - a class with an abstract method become abstract too.
 - there is no reason for a method to be abstract because its class is
 abstract.
100% agree (Java abstract semantics).
Aug 22 2003
parent Benji Smith <dlanguage xxagg.com> writes:
In article <bi5bge$13nv$4 digitaldaemon.com>, Mike Wynn says...
"DeadCow" <deadcow-remove-this free.fr> wrote in message
 Here is my point of view:

 - an abstract method is a "not implemented" method.
 - an abstract class is a "not concrete" class. Something you can derive 
from
 but that you cant instanciate.
 - a class with an abstract method become abstract too.
 - there is no reason for a method to be abstract because its class is
 abstract.
100% agree (Java abstract semantics).
Me too.
Aug 22 2003
prev sibling next sibling parent "Carlos Santander B." <carlos8294 msn.com> writes:
"DeadCow" <deadcow-remove-this free.fr> wrote in message
news:bi46nj$2ehh$1 digitaldaemon.com...
|
| - an abstract method is a "not implemented" method.
| - an abstract class is a "not concrete" class. Something you can derive
from
| but that you cant instanciate.
| - a class with an abstract method become abstract too.
| - there is no reason for a method to be abstract because its class is
| abstract.
|

I also agree with that

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 2003-08-19
Aug 22 2003
prev sibling parent reply "Lars Ivar Igesund" <larsivi stud.ntnu.no> writes:
 Of course not. Here is my point of view:

 - an abstract method is a "not implemented" method.
 - an abstract class is a "not concrete" class. Something you can derive
from
 but that you cant instanciate.
 - a class with an abstract method become abstract too.
 - there is no reason for a method to be abstract because its class is
 abstract.
I agree. Abstract should be a non-instantiation modifier, not no-implementation. Lars Ivar Igesund
Aug 24 2003
next sibling parent reply "Matthew Wilson" <dmd synesis.com.au> writes:
I agree

"Lars Ivar Igesund" <larsivi stud.ntnu.no> wrote in message
news:bia0op$28bb$1 digitaldaemon.com...
 Of course not. Here is my point of view:

 - an abstract method is a "not implemented" method.
 - an abstract class is a "not concrete" class. Something you can derive
from
 but that you cant instanciate.
 - a class with an abstract method become abstract too.
 - there is no reason for a method to be abstract because its class is
 abstract.
I agree. Abstract should be a non-instantiation modifier, not no-implementation. Lars Ivar Igesund
Aug 24 2003
parent reply "DeadCow" <deadcow-remove-this free.fr> writes:
"Matthew Wilson" <dmd synesis.com.au> a écrit dans le message news:
bia57h$2eu0$1 digitaldaemon.com...
 I agree
Is Walter agree too ? Can we hope it changed by DMD 0.71 ? -- Nicolas Repiquet
Aug 25 2003
parent "Walter" <walter digitalmars.com> writes:
"DeadCow" <deadcow-remove-this free.fr> wrote in message
news:bida9d$14p9$1 digitaldaemon.com...
 "Matthew Wilson" <dmd synesis.com.au> a écrit dans le message news:
 bia57h$2eu0$1 digitaldaemon.com...
 I agree
Is Walter agree too ? Can we hope it changed by DMD 0.71 ?
Y'all are right. I'll fix it.
Aug 31 2003
prev sibling parent reply Ilya Minkov <midiclub 8ung.at> writes:
Lars Ivar Igesund wrote:

 I agree. Abstract should be a non-instantiation modifier, not
 no-implementation.
Then what is it good for, since abstract is also implicit when some function is not defined, and also means the class is not instantiatable? -eye
Sep 01 2003
parent reply "Lars Ivar Igesund" <larsivi stud.ntnu.no> writes:
"Ilya Minkov" <midiclub 8ung.at> wrote:
 Lars Ivar Igesund wrote:

 I agree. Abstract should be a non-instantiation modifier, not
 no-implementation.
Then what is it good for, since abstract is also implicit when some function is not defined, and also means the class is not instantiatable?
I got used to the 'abstract' keyword in Java. There it means just what I said above. Anyway; A method don't have to be implemented, in which case it must be abstract (or part of an interface). Since an instantiatable class needs all it's methods to be implemented, a class with an abstract method must be abstract. If all methods are implemented, the class can still be abstract. This typically happens when you need a base class with lots of functionality which is inherited into several subclasses, but it doesn't make sense to instantiate an instance of the baseclass itself. Possible example: A scenegraph library where there is a base node. If this scenegraph use reference counting, the nodes will have ref() and unref() methods that will use the same implementation for all subclasses. Thus they are best implemented in the BaseNode class, but you don't want to instantiate it since it don't have any useful functionality. You don't want it as an interface either, since it then must be implemented in the subclasses. Lars Ivar
Sep 01 2003
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Lars Ivar Igesund" <larsivi stud.ntnu.no> wrote in message
news:bj08cm$2fl6$1 digitaldaemon.com...
|
| ...
| If all methods are implemented, the class can still be abstract. This
| typically happens when you need a base class with lots of functionality
| which is inherited into several subclasses, but it doesn't make sense to
| instantiate an instance of the baseclass itself.
| ...

I agree with that use of abstract (that's what I first expected), but if
that gets into the language, what would be the difference between these two?

abstract class A {
    void foo();
    int bar(char []);
}

interface B {
    void foo();
    int bar(char []);
}

And I mean in a pure non-implementation sense. I mean, I don't know about
vtables and such, so don't go that way. (I might be missing something, but I
still want to know)

—————————————————————————
Carlos Santander


---

Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.514 / Virus Database: 312 - Release Date: 2003-08-28
Sep 01 2003
next sibling parent "DeadCow" <deadcow-remove-this free.fr> writes:
"Carlos Santander B." <carlos8294 msn.com> a écrit dans le message news:
bj0e0h$2n9r$2 digitaldaemon.com...

 I agree with that use of abstract (that's what I first expected), but if
 that gets into the language, what would be the difference between these
two?
 [snip]
 And I mean in a pure non-implementation sense. I mean, I don't know about
 vtables and such, so don't go that way. (I might be missing something, but
I
 still want to know)
Well, I think its just an OO design difference. B extends A means A *is a* B. B implements A means A *can act like a* B. -- Nicolas Repiquet
Sep 01 2003
prev sibling parent reply "Philippe Mori" <philippe_mori hotmail.com> writes:
 I agree with that use of abstract (that's what I first expected), but if
 that gets into the language, what would be the difference between these
two?
 abstract class A {
     void foo();
     int bar(char []);
 }

 interface B {
     void foo();
     int bar(char []);
 }

 And I mean in a pure non-implementation sense. I mean, I don't know about
 vtables and such, so don't go that way. (I might be missing something, but
I
 still want to know)
As it, IMO, there are no differences between the abstract class and the interface but there will be difference if you want data member or MI. - You can derives from multiples interface but only one class (abstract or not). - An interface cannot have any data member but an abstract class can. - An abstract class will probably allows to define member functions as mentionned elsewhere. So the main difference would be the intended use. An abstract class might serve as the base class of a class hierarchy and offer some services to derives classes. An interface only declare function that need to be implemented to support it...
Sep 01 2003
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Philippe Mori" <philippe_mori hotmail.com> wrote in message
news:bj1056$elk$1 digitaldaemon.com...
|
| As it, IMO, there are no differences between the abstract class and the
| interface but there will be difference if you want data member or MI.
|
| - You can derives from multiples interface but only one class (abstract or
| not).
| - An interface cannot have any data member but an abstract class can.
| - An abstract class will probably allows to define member functions as
| mentionned elsewhere.
|
| So the main difference would be the intended use. An abstract class
| might serve as the base class of a class hierarchy and offer some services
| to derives classes. An interface only declare function that need to be
| implemented to support it...
|

Yes, that I understand: that's the difference between interfaces and
abstract classes, but I was talking that specific case where both an
abstract class and an interface where declared exactly the same.

—————————————————————————
Carlos Santander


---

Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.515 / Virus Database: 313 - Release Date: 2003-09-01
Sep 01 2003
parent "Lars Ivar Igesund" <larsivi stud.ntnu.no> writes:
 Yes, that I understand: that's the difference between interfaces and
 abstract classes, but I was talking that specific case where both an
 abstract class and an interface where declared exactly the same.
In this case, the difference is just that one is abstract and the other an interface. In that case, maybe you should choose to use an interface, as you then keep the superclass slot open for the class inheriting. I think the concept of abstract classes is useful mostly when theyre partially or fully implemented. Lars Ivar
Sep 02 2003
prev sibling parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
Isn't that name a little pretentious?  Besides people will think you're
Digital Mars tech support.  You could get Walter in legal trouble.

Walter you may want to have tboon here change his nick?

Sean

"Digital Mars" <tboon gte.net> wrote in message
news:bi368j$t64$1 digitaldaemon.com...
 You can have abstract classes in (at least) two ways. Either declare the
 entire class abstract, which means that there are no implementations in
the
Aug 22 2003