Polymorphism: Difference between revisions

Content added Content deleted
(Scala contribution added.)
Line 3,796: Line 3,796:
puts d # => Circle at 4,5 with radius 7.5</lang>
puts d # => Circle at 4,5 with radius 7.5</lang>


=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/altUDTl/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/ohfeM4nvTZyeOTYYWHaNgQ Scastie (remote JVM)].
<lang Scala>object PointCircle extends App {

class Point(x: Int = 0, y: Int = 0) {

def copy(x: Int = this.x, y: Int = this.y): Point = new Point(x, y)

override def toString = s"Point x: $x, y: $y"
}

object Point {
def apply(x: Int = 0, y: Int = 0): Point = new Point(x, y)
}

case class Circle(x: Int = 0, y: Int = 0, r: Int = 0) extends Point(x, y) {

def copy(r: Int): Circle = Circle(x, y, r)

override def toString = s"Circle x: $x, y: $y, r: $r"
}

val p = Point()
val c = Circle()
println("Instantiated ", p)
println("Instantiated ", c)

val q = Point(5, 6)
println("Instantiated ", q)
val r = q.copy(y = 7) // change y coordinate
println(r, " changed y coordinate")

val d = Circle(5, 6, 7)
println("Instantiated ", d)
val e = d.copy(r = 8) // change radius
println(e, " changed radius")

}</lang>
=={{header|Seed7}}==
=={{header|Seed7}}==
[http://seed7.sourceforge.net/manual/objects.htm Seed7 object orientation] works via interfaces.
[http://seed7.sourceforge.net/manual/objects.htm Seed7 object orientation] works via interfaces.
The example below introduces the interface type ''GraphicObj''. To be usable an interface type needs also interface functions (which are defined with the keyword DYNAMIC). The interface function ''print'' is defined for ''GraphicObj''. The struct types ''Point'' and ''Circle'' implement the the interface ''GraphicObj'' (they are implementation types).
The example below introduces the interface type ''GraphicObj''.
To be usable an interface type needs also interface functions (which are defined with the keyword DYNAMIC).
The interface function ''print'' is defined for ''GraphicObj''.
The struct types ''Point'' and ''Circle'' implement the the interface ''GraphicObj'' (they are implementation types).
Note that ''Circle'' inherits ''x'' and ''y'' from ''Point''.
Note that ''Circle'' inherits ''x'' and ''y'' from ''Point''.
Functions which return a ''Point'' respectively ''Circle'' are used as constructors.
Functions which return a ''Point'' respectively ''Circle'' are used as constructors.
Note that a Seed7 constructor does not need to have the name of the type (a new Point could be created with a function called abc).
Note that a Seed7 constructor does not need to have the name of the type (a new Point could be created with a function called abc).
Seed7 defines copy constructor, assignment and destructor automatically.
Seed7 defines copy constructor, assignment and destructor automatically.

<lang seed7>$ include "seed7_05.s7i";
<lang seed7>$ include "seed7_05.s7i";


Line 3,869: Line 3,903:


{{out}}
{{out}}
Circle(3, 4, 5)
<pre>
Point(1, 2)
Circle(3, 4, 5)
</pre>


=={{header|Self}}==
=={{header|Self}}==