One thing that distinguishes an "abstract" class from a concrete one is that you shouldn't create instances the abstract class. There's no need for special syntax for this. You need merely do something like:
And then rely on subclasses to implement their own new. Unfortunately, this confounds the common (and useful) idiom of inheriting new from a (possibly abstract) superclass.package AbstactFoo; ... sub new { die "Don't create any Abstract Foos direct, fool!"; }
Here's a way to get the benifit of an "abstract" new while still keeping the class abstract:package AbstractFoo; ... sub new { my $package = shift; bless { @_ }, $package; } ... my $foo1 = new SubclassOfAbstractFoo(); # GOOD my $foo2 = new AbstractFoo(); # BAD
package AbstractFoo; ... sub new { my $package = shift; die "Don't subclass AbstractFoo directly!" if $package eq "AbstractFoo"; bless { @_ }, $package; }
(Bonus points to whoever sees a way to generalize this even further.)
In reply to Re: Why do you need abstract classes in Perl?
by dws
in thread Why do you need abstract classes in Perl?
by jeffa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |