Sealed classes and methods: Difference between revisions

m
Promoted to ‘full’ task
m (→‎{{header|Wren}}: Changed to Wren S/H)
m (Promoted to ‘full’ task)
(2 intermediate revisions by 2 users not shown)
Line 1:
{{draft task}}
 
;Definition
In [[wp:Object-oriented_programming|object-oriented programming]], a '''sealed''' or '''final class''' is one which cannot be inherited from.
 
Classes are sometimes made non-subclasssablesubclassable in this way if the author feels that it would not be useful or even undesirable for subclasses to be created from them. Moreover, in a compiled language, knowing that a class cannot be subclassed, may enable optimizations to be made.
 
Rather than sealing the entire class, it may be possible to just seal certain methods for the same reasons and with the same potential benefits.
Line 368:
<!--</syntaxhighlight>-->
Note however in Phix there is no possibility of invoking parent.watch_movie() from the else branch, because you have completely replaced that routine in the child instance. Should you want to share code in that kind of fashion you would need to give it a different and completely unambiguous name.
 
=={{header|Raku}}==
Raku doesn't have final class but [https://docs.raku.org/syntax/role Roles] is normally used to mimic the goal.
<syntaxhighlight lang="raku" line># 20240626 Raku programming solution
 
role MovieWatcherRole { has Str $.name;
method WatchMovie() { say "$.name is watching the movie" }
method EatPopcorn() { say "$.name is enjoying the popcorn" }
}
 
class MovieWatcher does MovieWatcherRole {
method new(Str $name) { self.bless(:$name) }
}
 
class ParentMovieWatcher is MovieWatcher {
method new(Str $name) { self.bless(:$name) }
}
 
role ChildMovieWatcherRole {
method EatPopcorn() { say "$.name is eating too much popcorn" }
}
 
class ChildMovieWatcher is MovieWatcher does ChildMovieWatcherRole {
method new(Str $name) { self.bless(:$name) }
}
 
role YoungChildMovieWatcherRole {
method WatchMovie() {
say "Sorry, $.name, you are too young to watch the movie.";
}
}
 
class YoungChildMovieWatcher is ChildMovieWatcher does YoungChildMovieWatcherRole {
method new(Str $name) { self.bless(:$name) }
}
 
for ParentMovieWatcher.new('Donald'),
ChildMovieWatcher.new('Lisa'),
YoungChildMovieWatcher.new('Fred')
{ .WatchMovie and .EatPopcorn }</syntaxhighlight>
 
You may [https://ato.pxeger.com/run?1=nVPBToNAED144ysmxKRtgpyNPVo9adLYg_G4wlBQ2Gl2FxvS8CVeetCf8uKvuLtAoIVIldNm9r15bx6z7x-Cveb7_WeuoovLr7NvQSnCPb0l-MhUEKN4MIUdxEzCSgk49znLcO4AQIYqphAszjKmMw2UrAC3QkEiYWtuE74GFSNkBuVqatnh3zC1pE1Agg_xkb9Q0fA3Fc7V_NJxgpRJeeAVQkI54L6jxnE7tXMYAauHaeQ_pyjl9KoudrovmUCuDjSSI81_dbcxX8dJGv7qdiQbpmwyRJDlQTwYT0-j599mNm7lb6M9Uc7X402Pdsfc6M-OuSIhCq_eNw8KykH_CztsYZrrU7Vc7Wb57ty26Iw_7MNk0C_aIE40fnIaEYmBJfINf7IgztJwMvPs3D3RCnSXSNZAhs1VuFuBupWzA78NFRgPwW-XCMrqndfPvXn2Pw Attempt This Online!]
 
=={{header|Wren}}==
9,490

edits