Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring SUPER to superclass of object

Please see the perldoc for oop http://perldoc.perl.org/perlobj.html

As per the document: "It is important to note that SUPER refers to the superclass(es) of the current package and not to the superclass(es) of the object."

Now, I am in a situation where I need the SUPER to refer to the superclass(es) of the object.

So, looking for any way to achieve it.

like image 610
rpg Avatar asked Jan 12 '11 14:01

rpg


People also ask

Can a subclass reference hold a superclass object?

So difference between referencing using superclass reference and referencing using subclass reference is use superclass referencing can holds object of subclass and could only access the methods which are defined/overridden by subclass while use subclass referencing can not hold object of superclass and could access ...

Can we call super class method using subclass object?

Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).

What is the superclass of object?

Object: Java's superclass Object is the root class, or ultimate superclass, of all other Java classes. Stored in the java. lang package, Object declares the following methods, which all other classes inherit: protected Object clone()

Is super () called automatically?

Use of super() to access superclass constructor As we know, when an object of a class is created, its default constructor is automatically called. To explicitly call the superclass constructor from the subclass constructor, we use super() .


2 Answers

I suggest you look into the SUPER module.

like image 125
ivancho Avatar answered Oct 13 '22 11:10

ivancho


It's fairly rare that you really need to do something like this, usually it's a sign that your mucking about inside an object in a way that will come back to bite you later. If it's really what needs to be done you can change your package just for the method call to change what SUPER sees, or override method look up by calling the full method name.

{
    package BaseClass;
    sub new { bless \my $self, shift; }
    sub foo { 
        my $self = shift; 
        print "BaseClass::foo()\n";
    }
}
{
    package SubClass;
    our @ISA = qw(BaseClass);
    sub foo { 
        my $self = shift; 
        print "SubClass::foo()\n"; 
        $self->SUPER::foo(); 
    }
}
{
    package ParentClass;
    sub new { bless \my $self, shift; }
    sub bar { 
        my $self = shift; 
        print "ParentClass::bar()\n"; 
    }
}
{
    package ChildClass;
    our @ISA = qw(ParentClass);
    sub foo {
        my $other = SubClass->new();
        print "ChildClass::foo()\n";
        # fails trying to find ParentClass::foo()
        eval { $other->SUPER::foo(); } or warn $@;
        # thinks this is SubClass and finds BaseClass::foo()
        { package SubClass; $other->SUPER::foo(); }
        # if you know the correct class that SUPER::foo() would have called (but this will also work if it was the wrong class)
        $other->BaseClass::foo();
    }
    sub bar { 
        my $self = shift; 
        print "ChildClass::bar()\n"; 
        $self->SUPER::bar(); 
    }
}

my $obj_1 = SubClass->new();
$obj_1->foo();

my $obj_2 = ChildClass->new();
$obj_2->bar();
$obj_2->foo();

A cleaner option is to refactor your methods so that you can access both the base class and sub class methods without trying to subvert the object system.

{
    package BaseClass;
    sub new { bless \my $self, shift; }
    sub foo { 
        my $self = shift; 
        print "BaseClass::foo()\n"; 
    }
}
{
    package SubClass;
    our @ISA = qw(BaseClass);
    sub bar { 
        my $self = shift; 
        print "SubClass::bar()\n"; 
        $self->SUPER::foo(); 
    }
}

my $obj = SubClass->new();
$obj->foo();
$obj->bar();

Or provide a method to call the base class method.

{
    package BaseClass;
    sub new { bless \my $self, shift; }
    sub foo { 
        my $self = shift; 
        print "BaseClass::foo()\n"; 
    }
}
{
    package SubClass;
    our @ISA = qw(BaseClass);
    sub foo { 
        my $self = shift; 
        print "SubClass::foo()\n"; 
        $self->SUPER::foo(); 
    }
    sub bar { 
        my $self = shift; 
        $self->SUPER::foo(); 
    }
}

my $obj = SubClass->new();
$obj->foo();
$obj->bar();

The best answer really depends on what you are really trying to do an why that requires you to work around standard inheritance.

like image 20
Ven'Tatsu Avatar answered Oct 13 '22 10:10

Ven'Tatsu