Jump to page: 1 2 3
Thread overview
abstract problems
Aug 20, 2003
DeadCow
Aug 21, 2003
Digital Mars
Aug 21, 2003
DeadCow
Aug 21, 2003
Digital Mars
Aug 21, 2003
DeadCow
Aug 22, 2003
Philippe Mori
Aug 22, 2003
DeadCow
Aug 22, 2003
Mike Wynn
Aug 22, 2003
Benji Smith
Aug 24, 2003
Lars Ivar Igesund
Aug 24, 2003
Matthew Wilson
Aug 25, 2003
DeadCow
Aug 31, 2003
Walter
Sep 01, 2003
Ilya Minkov
Sep 01, 2003
Lars Ivar Igesund
Sep 01, 2003
DeadCow
Sep 02, 2003
Philippe Mori
Sep 02, 2003
Lars Ivar Igesund
Aug 22, 2003
Sean L. Palmer
August 20, 2003
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


August 21, 2003
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
>
>


August 21, 2003
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


August 21, 2003
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
>
>


August 21, 2003
"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


August 22, 2003
>
> 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...


August 22, 2003
"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


August 22, 2003
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


August 22, 2003
"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).




August 22, 2003
"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


« First   ‹ Prev
1 2 3