1

I want to implement dynamic polymorphism in python like in java. Is it possible? if yes please guide me how.I want to implement something like this:

class A {
    public void show() {
        System.out.println("Base class");
    }
}

class B extends A{
    public void show() {
        System.out.println("Derived class");
    }
} 

class C {
    public static void main(String args[]) {
        A a = new B();
        a.show(); // so it will call B's show method. 
    }
}
2
  • Didn't you ment for the B class to be derived from A? Because the code says it's not. Commented Jan 2, 2014 at 11:07
  • @JanSpurny thank you for pointing that out.I forgot to extend the A class.Now it's updated. Commented Jan 2, 2014 at 11:36

4 Answers 4

2

Of course it is possible. Actually Python is even more powerful in this sense because it has only dynamic types. In the example below you do not even need to derive B from A.

class A:
    def show(self):
        print "Base class"

class B(A):  # this would also work fine without deriving: class B: 
    def show(self):
        print "Derived class"

a = A()
a.show()
a = B()
a.show()

When interpretter meets the expression a.show(), it only looks up the object 'a' and checks if it has method 'show' implemented. If yes, then it runs this method regardless of inheritance or whatever. If 'show' is not found, then exception is raised.

Sign up to request clarification or add additional context in comments.

Comments

2

Your issue is here:

A a = new B();

Because Python is dynamic, there is no way to declare that a variable is of type A, hence after a = B() the name a will always be bound to a B object until you delete or rebind the name.

Comments

0

To me, the question seems irrelevant. Since Python uses "duck typing" for classes, the show method for the class you actually create an instance of would be the one called.

Duck typing:

http://en.wikipedia.org/wiki/Duck_typing

Comments

0

In Python (2.x) you could do something like:

class A(object): # inherit from base object
    def show(self): # define function to inherit
        print "Base class"

class B(A): # inherit from A
    def show(self): # override A.show()
        print "Derived class"

a = B() # instance of derived class B
a.show() # would print "Derived class"

Note that, due to the inheritance from A in B:

isinstance(a, A) == True

Alternatively, you could do:

a = A() # instance of base class A
a.show = B().show # use B's method for this instance of A

3 Comments

I think you just created an object of class B and if you call show method on B's object it will always call B's show method.and since B inherits A any object of B will be an instance of A.That doesn't answer my question here.
I have added an alternative, where you use B's show for an A instance; is that what you had in mind?
it's just a way around of it.what i actually want is to be able to call all the overloaded methods of derived class from base class object(or something similar to base class reference in JAVA.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.