Discussion:
Abstract classes and virtual methods
Renaud Bardet
2009-12-11 14:36:23 UTC
Permalink
Hi list,

For the language developpers :

There is no abstract classes nor pure virtual function (virtual = 0 in C++)
in HaXe,
is it a choice or a constraint ?

Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
renaud.bardet-+nuTRvZ9+***@public.gmane.org
--
Achmad Aulia
2009-12-11 21:03:44 UTC
Permalink
You mean "interface" ?
Post by Renaud Bardet
Hi list,
There is no abstract classes nor pure virtual function (virtual = 0 in C++)
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
Pimm Hogeling
2009-12-12 09:26:18 UTC
Permalink
Achmad's reply was epic. We had a good laugh.

It is a choice.
Post by Achmad Aulia
You mean "interface" ?
Post by Renaud Bardet
Hi list,
There is no abstract classes nor pure virtual function (virtual = 0 in
C++)
Post by Renaud Bardet
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
信手涂鸦站长
2009-12-12 09:36:26 UTC
Permalink
It is different with actionscript 3, haxe allow you to define
variables in interface , I think the developers maybe thought we don't
need Abstract Classes with this feature ? But in fact , we sometimes
need Abstract Classes , and Consts etc ...

btw: There is no User Define Abstract Classes too in Actionscript 3 .
Post by Pimm Hogeling
Achmad's reply was epic. We had a good laugh.
It is a choice.
Post by Achmad Aulia
You mean "interface" ?
Post by Renaud Bardet
Hi list,
There is no abstract classes nor pure virtual function (virtual = 0 in C++)
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards

Yin Fei

Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
Ian Liu Rodrigues
2009-12-12 13:27:48 UTC
Permalink
Classes with private constructors are abstract, since you can instantiate an
object, forcing you to extend it to make it useful.

Ian L.
Post by 信手涂鸦站长
It is different with actionscript 3, haxe allow you to define
variables in interface , I think the developers maybe thought we don't
need Abstract Classes with this feature ? But in fact , we sometimes
need Abstract Classes , and Consts etc ...
btw: There is no User Define Abstract Classes too in Actionscript 3 .
Post by Pimm Hogeling
Achmad's reply was epic. We had a good laugh.
It is a choice.
Post by Achmad Aulia
You mean "interface" ?
Post by Renaud Bardet
Hi list,
There is no abstract classes nor pure virtual function (virtual = 0 in C++)
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards
Yin Fei
Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
Ian Liu Rodrigues
2009-12-12 13:28:55 UTC
Permalink
I meant you *can't instantiate it*
*;)
*
Post by Ian Liu Rodrigues
Classes with private constructors are abstract, since you can instantiate
an object, forcing you to extend it to make it useful.
Ian L.
It is different with actionscript 3, haxe allow you to define
Post by 信手涂鸦站长
variables in interface , I think the developers maybe thought we don't
need Abstract Classes with this feature ? But in fact , we sometimes
need Abstract Classes , and Consts etc ...
btw: There is no User Define Abstract Classes too in Actionscript 3 .
Post by Pimm Hogeling
Achmad's reply was epic. We had a good laugh.
It is a choice.
Post by Achmad Aulia
You mean "interface" ?
Post by Renaud Bardet
Hi list,
There is no abstract classes nor pure virtual function (virtual = 0
in
Post by Pimm Hogeling
Post by Achmad Aulia
Post by Renaud Bardet
C++)
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards
Yin Fei
Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
Achmad Aulia
2009-12-12 13:40:20 UTC
Permalink
Well it's been a while since I use my C++ skill (I originally C++
programmer), but IIRC Abstract Base Classes (ABC) is basicly a class, with
all the method set to pure virtual, which is basically the same as
interface. And usually used the same way, to establish "contract" between
different part of the code, and programmer.
Post by Ian Liu Rodrigues
I meant you *can't instantiate it*
*;)
*
Classes with private constructors are abstract, since you can instantiate
Post by Ian Liu Rodrigues
an object, forcing you to extend it to make it useful.
Ian L.
It is different with actionscript 3, haxe allow you to define
Post by 信手涂鸦站长
variables in interface , I think the developers maybe thought we don't
need Abstract Classes with this feature ? But in fact , we sometimes
need Abstract Classes , and Consts etc ...
btw: There is no User Define Abstract Classes too in Actionscript 3 .
Post by Pimm Hogeling
Achmad's reply was epic. We had a good laugh.
It is a choice.
Post by Achmad Aulia
You mean "interface" ?
Post by Renaud Bardet
Hi list,
There is no abstract classes nor pure virtual function (virtual = 0
in
Post by Pimm Hogeling
Post by Achmad Aulia
Post by Renaud Bardet
C++)
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - DšŠveloppeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards
Yin Fei
Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
Ian Liu Rodrigues
2009-12-12 14:07:09 UTC
Permalink
Abstract classes can provide implementation at some degree.
Below is an example. The abstract class provides a constructor
which is common to both subclasses, but doesn't care about 'area'
method, which should be overridden.
Note that, as haXe doesn't have an abstract keyword, the method
must be complete. That means it must return a value if the function
demands it.

class AbstractPolygon {
var width:Float;
var height:Float;
private function new(width:Float, height:Float) {
this.width = width;
this.height = height;
}
public function area():Float return 0; // abstract method
}

class Triangle extends AbstractPolygon {
public function new(width:Float, height:Float) {
super(width, height);
}
public override function area():Float {
return width * height / 2;
}
}

class Rectangle extends AbstractPolygon {
public function new(width:Float, height:Float) {
super(width, height);
}
public override function area():Float {
return width * height;
}
}

class Main {
static function main() {
var p1:AbstractPolygon = new Triangle(2, 5);
var p2:AbstractPolygon = new Rectangle(2, 5);
trace(p1.area());
trace(p2.area());
}
}

The example was taken from C++ tutorial on cplusplus.com as I'm learning
that now =P

Ian L.
Post by Achmad Aulia
Well it's been a while since I use my C++ skill (I originally C++
programmer), but IIRC Abstract Base Classes (ABC) is basicly a class, with
all the method set to pure virtual, which is basically the same as
interface. And usually used the same way, to establish "contract" between
different part of the code, and programmer.
Post by Ian Liu Rodrigues
I meant you *can't instantiate it*
*;)
*
Classes with private constructors are abstract, since you can instantiate
Post by Ian Liu Rodrigues
an object, forcing you to extend it to make it useful.
Ian L.
It is different with actionscript 3, haxe allow you to define
Post by 信手涂鸦站长
variables in interface , I think the developers maybe thought we don't
need Abstract Classes with this feature ? But in fact , we sometimes
need Abstract Classes , and Consts etc ...
btw: There is no User Define Abstract Classes too in Actionscript 3 .
Post by Pimm Hogeling
Achmad's reply was epic. We had a good laugh.
It is a choice.
Post by Achmad Aulia
You mean "interface" ?
Post by Renaud Bardet
Hi list,
There is no abstract classes nor pure virtual function (virtual = 0
in
Post by Pimm Hogeling
Post by Achmad Aulia
Post by Renaud Bardet
C++)
in HaXe,
is it a choice or a constraint ?
Cheers,
--
Renaud Bardet - Développeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards
Yin Fei
Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
Juraj Kirchheim
2009-12-12 17:49:13 UTC
Permalink
Post by Ian Liu Rodrigues
Abstract classes can provide implementation at some degree.
Below is an example. The abstract class provides a constructor
which is common to both subclasses, but doesn't care about 'area'
method, which should be overridden.
Note that, as haXe doesn't have an abstract keyword, the method
must be complete. That means it must return a value if the function
demands it.
yes. the most simple thing to do is to write

return throw "abstract"

or, a little better (just create a snippet for this (in case your IDE of
choice provides that functionallity)):

return throw "abstract method
"+here.className+"::"+here.methodName+" not implemented by
"+Type.getClassName(Type.getClass(this));

When it comes to good haXe examples for abstract base classes, I'd like
to point out haxe.io.Input and haxe.io.Output.
They are abstract, yet they provide a lot of functionality.

greetz
back2dos
--
haXe - an open source web programming language
http://haxe.org
Iman Khabazian
2009-12-13 00:05:59 UTC
Permalink
I think the point is its helpful to catch it at compile time, so if you
classify a function as abstract and its not overridden you get the compile
time error instead of the run time error.
Post by Juraj Kirchheim
Post by Ian Liu Rodrigues
Abstract classes can provide implementation at some degree.
Below is an example. The abstract class provides a constructor
which is common to both subclasses, but doesn't care about 'area'
method, which should be overridden.
Note that, as haXe doesn't have an abstract keyword, the method
must be complete. That means it must return a value if the function
demands it.
yes. the most simple thing to do is to write
return throw "abstract"
or, a little better (just create a snippet for this (in case your IDE of
return throw "abstract method "+here.className+"::"+here.methodName+" not
implemented by "+Type.getClassName(Type.getClass(this));
When it comes to good haXe examples for abstract base classes, I'd like to
point out haxe.io.Input and haxe.io.Output.
They are abstract, yet they provide a lot of functionality.
greetz
back2dos
--
haXe - an open source web programming language
http://haxe.org
信手涂鸦站长
2009-12-13 03:26:55 UTC
Permalink
Yes , there is a compile error when we define a private constructor .
like this :
''
Cannot access private constructor
''
So we can use it to instead abstract class most times .

And this is more useful in some patterns like singleton and multiton . ex:

class SingletonExample
{
private static var _instance: SingletonExample;
private function new()
{

}

public function getInstance(): SingletonExample
{
...
}
}
Post by Iman Khabazian
I think the point is its helpful to catch it at compile time, so if you
classify a function as abstract and its not overridden you get the compile
time error instead of the run time error.
Post by Juraj Kirchheim
Post by Ian Liu Rodrigues
Abstract classes can provide implementation at some degree.
Below is an example. The abstract class provides a constructor
which is common to both subclasses, but doesn't care about 'area'
method, which should be overridden.
Note that, as haXe doesn't have an abstract keyword, the method
must be complete. That means it must return a value if the function
demands it.
yes. the most simple thing to do is to write
  return throw "abstract"
or, a little better (just create a snippet for this (in case your IDE of
  return throw "abstract method "+here.className+"::"+here.methodName+"
not implemented by "+Type.getClassName(Type.getClass(this));
When it comes to good haXe examples for abstract base classes, I'd like to
point out haxe.io.Input and haxe.io.Output.
They are abstract, yet they provide a lot of functionality.
greetz
back2dos
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards

Yin Fei

Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
Renaud Bardet
2009-12-14 09:37:14 UTC
Permalink
Hi,

the private constructor indeed is helpfull, I didn't even thought about it
as in most languages private funct and vars are inaccessible in subclasses,
the HaXe 'private' actually acts more like a 'protected'
Post by 信手涂鸦站长
Yes , there is a compile error when we define a private constructor .
''
Cannot access private constructor
''
So we can use it to instead abstract class most times .
class SingletonExample
{
private static var _instance: SingletonExample;
private function new()
{
}
public function getInstance(): SingletonExample
{
...
}
}
Post by Iman Khabazian
I think the point is its helpful to catch it at compile time, so if you
classify a function as abstract and its not overridden you get the
compile
Post by Iman Khabazian
time error instead of the run time error.
Post by Juraj Kirchheim
Post by Ian Liu Rodrigues
Abstract classes can provide implementation at some degree.
Below is an example. The abstract class provides a constructor
which is common to both subclasses, but doesn't care about 'area'
method, which should be overridden.
Note that, as haXe doesn't have an abstract keyword, the method
must be complete. That means it must return a value if the function
demands it.
yes. the most simple thing to do is to write
return throw "abstract"
or, a little better (just create a snippet for this (in case your IDE of
return throw "abstract method "+here.className+"::"+here.methodName+"
not implemented by "+Type.getClassName(Type.getClass(this));
When it comes to good haXe examples for abstract base classes, I'd like
to
Post by Iman Khabazian
Post by Juraj Kirchheim
point out haxe.io.Input and haxe.io.Output.
They are abstract, yet they provide a lot of functionality.
greetz
back2dos
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards
Yin Fei
Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
--
Renaud Bardet - DšŠveloppeur
Succubus Interactive
renaud.bardet-+nuTRvZ9+***@public.gmane.org
--
Mark de Bruijn | Dykam
2009-12-14 22:11:18 UTC
Permalink
In haXe, private is protected.
Post by Renaud Bardet
Hi,
the private constructor indeed is helpfull, I didn't even thought about it
as in most languages private funct and vars are inaccessible in subclasses,
the HaXe 'private' actually acts more like a 'protected'
Yes , there is a compile error when we define a private constructor .
Post by 信手涂鸦站长
''
Cannot access private constructor
''
So we can use it to instead abstract class most times .
class SingletonExample
{
private static var _instance: SingletonExample;
private function new()
{
}
public function getInstance(): SingletonExample
{
...
}
}
Post by Iman Khabazian
I think the point is its helpful to catch it at compile time, so if you
classify a function as abstract and its not overridden you get the
compile
Post by Iman Khabazian
time error instead of the run time error.
Post by Juraj Kirchheim
Post by Ian Liu Rodrigues
Abstract classes can provide implementation at some degree.
Below is an example. The abstract class provides a constructor
which is common to both subclasses, but doesn't care about 'area'
method, which should be overridden.
Note that, as haXe doesn't have an abstract keyword, the method
must be complete. That means it must return a value if the function
demands it.
yes. the most simple thing to do is to write
return throw "abstract"
or, a little better (just create a snippet for this (in case your IDE
of
Post by Iman Khabazian
Post by Juraj Kirchheim
return throw "abstract method "+here.className+"::"+here.methodName+"
not implemented by "+Type.getClassName(Type.getClass(this));
When it comes to good haXe examples for abstract base classes, I'd like
to
Post by Iman Khabazian
Post by Juraj Kirchheim
point out haxe.io.Input and haxe.io.Output.
They are abstract, yet they provide a lot of functionality.
greetz
back2dos
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
--
Best regards
Yin Fei
Icebirds.net
--
haXe - an open source web programming language
http://haxe.org
--
Renaud Bardet - DšŠveloppeur
Succubus Interactive
--
--
haXe - an open source web programming language
http://haxe.org
--
Mark
Loading...