Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
Classes, polymorphism and subclassing
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. Classes, polymorphism and subclassing
2. Polymorphism
Polymorphism, from the Greek, means having many forms.
In programming, object-oriented programming allows polymorphism.
This page provides some examples in various languages of inheritance and polymorphism.
3. Python
In the following, the class
Verse2
is a subclass of class
Verse1
.
Here is the Python code.
class Verse1(): def __init__(self): self.name1 = "Mary" self.size1 = "little" self.animal1 = "lamb" def show(self): print("{0:s} had a {1:s} {2:s},".format(self.name1,self.size1,self.animal1)) class Verse2(Verse1): def __init__(self): super(Verse2,self).__init__() self.name1 = "John" self.animal1 = "pig" def show(self): print("(begin show)") super(Verse2,self).show() print("(end show)") verse1 = Verse1(); verse2 = Verse2(); verse2.size1 = "medium" verse1.show() verse2.show()
Here is the output of the Python code.
Mary had a little lamb, (begin show) John had a medium pig, (end show)
4. End of page
by RS
admin@ycp.powersoftwo.org