Python Forum
Polymorphism in Python Question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polymorphism in Python Question
#1
Coming from Java, I'm wondering how in Python you "program to interface, not to implementation", when Python doesn't have, well, interfaces?

For example, in Java, you could create a class hierarchy like Animal->Dog. Then, in code, you could create an object like this (pseud ocode):

Class Animal

Class Dog extends Animal
.
.
.
Animal animal = new Dog(); // polymorphism in Java
animal.makeSound();

Here, the variable dog has an Animal "type", but is an instance of a Dog class.

---

In Python, can you do the same type of thing?

class Animal():
def sound(self):
print("Basic Animal")

class Dog(Animal):
def sound(self):
print("Woof woof!")

===

How would you complete this code so it's polymorphic (using the Animal class to reference the dog class as in the Java example above)?

Thanks,
Reply


Messages In This Thread
Polymorphism in Python Question - by Oliver - Dec-13-2017, 02:10 PM
RE: Polymorphism in Python Question - by mpd - Dec-13-2017, 02:20 PM
RE: Polymorphism in Python Question - by hshivaraj - Dec-13-2017, 02:37 PM
RE: Polymorphism in Python Question - by Oliver - Dec-13-2017, 03:36 PM
RE: Polymorphism in Python Question - by hshivaraj - Dec-13-2017, 04:06 PM
RE: Polymorphism in Python Question - by Oliver - Dec-13-2017, 08:26 PM
RE: Polymorphism in Python Question - by metulburr - Dec-13-2017, 04:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Polymorphism not working with a call to a abstract method colt 3 2,367 Nov-04-2019, 11:04 PM
Last Post: colt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020