Jump to content

Polymorphism: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 2,009:
print(c::Circle) = println("Circle($(c.x), $(c.y), $(c.r))")
</lang>
 
=={{header|Kotlin}}==
Kotlin only has properties, not fields though the latter may be created by the compiler 'under the hood'. A 'get' accessor is automatically created for 'val' (read-only) properties and both get() and set() accessors for a 'var' (read/write) property though these may be overridden where appropriate.
 
Kotlin has the notion of a 'primary constructor' which is declared in the class header itself. It's also posible to create any number of 'secondary constructors' provided these delegate (directly or indirectly) to the primary constructor.
 
Although Kotlin supports operator overloading, it is not possible to overload the assignment operator ('=') itself.
 
In the JVM version of Kotlin, it is possible to declare a destructor in the guise of a 'finalize' method though there is no guarantee that this will actually be called by the garbage collector (or, if it is called, when this will be) and consequently many programmers feel it is more trouble than its worth.
<lang scala>// version 1.1.1
 
open class Point(var x: Int, var y: Int) {
constructor(): this(0, 0)
 
constructor(x: Int) : this(x, 0)
 
constructor(p: Point) : this(p.x, p.y)
 
open protected fun finalize() = println("Finalizing $this...")
 
override fun toString() = "Point at ($x, $y)"
 
open fun print() = println(this)
}
 
class Circle(x: Int, y: Int, var r: Int) : Point(x, y) {
constructor(): this(0, 0, 0)
 
constructor(x: Int) : this(x, 0, 0)
constructor(x: Int, r: Int) : this(x, 0, r)
 
constructor(c: Circle) : this(c.x, c.y, c.r)
 
// for simplicity not calling super.finalize() below though this would normally be done in practice
override protected fun finalize() = println("Finalizing $this...")
 
override fun toString() = "Circle at center ($x, $y), radius $r"
 
override fun print() = println(this)
}
 
fun createObjects() {
val points = listOf(Point(), Point(1), Point(2, 3), Point(Point(3, 4)))
for (point in points) point.print()
val circles = listOf(Circle(), Circle(1), Circle(2, 3), Circle(4, 5, 6), Circle(Circle(7, 8, 9)))
for (circle in circles) circle.print()
println()
}
 
fun main(args: Array<String>) {
createObjects()
System.gc() // try and force garbage collection
Thread.sleep(2000) // allow time for finalizers to run
println()
val p = Point(5, 6)
p.print()
p.y = 7 // change y coordinate
p.print()
val c = Circle(5, 6, 7)
c.print()
c.r = 8
c.print() // change radius
/* note that finalizers for p and c are not called */
}</lang>
 
{{out}}
<pre>
Point at (0, 0)
Point at (1, 0)
Point at (2, 3)
Point at (3, 4)
Circle at center (0, 0), radius 0
Circle at center (1, 0), radius 0
Circle at center (2, 0), radius 3
Circle at center (4, 5), radius 6
Circle at center (7, 8), radius 9
 
Finalizing Point at (3, 4)...
Finalizing Point at (3, 4)...
Finalizing Point at (2, 3)...
Finalizing Point at (1, 0)...
Finalizing Point at (0, 0)...
Finalizing Circle at center (7, 8), radius 9...
Finalizing Circle at center (7, 8), radius 9...
Finalizing Circle at center (4, 5), radius 6...
Finalizing Circle at center (2, 0), radius 3...
Finalizing Circle at center (1, 0), radius 0...
Finalizing Circle at center (0, 0), radius 0...
 
Point at (5, 6)
Point at (5, 7)
Circle at center (5, 6), radius 7
Circle at center (5, 6), radius 8
</pre>
 
=={{header|Lua}}==
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.