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,
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,