Sealed classes and methods: Difference between revisions
Content added Content deleted
m (Promoted to ‘full’ task) |
(New post.) |
||
Line 277: | Line 277: | ||
J does not provide a mechanism to seal individual methods. |
J does not provide a mechanism to seal individual methods. |
||
=={{header|Java}}== |
|||
Classes and methods can be sealed in Java by using the 'final' keyword. |
|||
<syntaxhighlight lang="java"> |
|||
import java.util.List; |
|||
public final class SealedClassesAndMethods { |
|||
public static void main(String[] args) { |
|||
List<MovieWatcher> movieWatchers = List.of( new ParentMovieWatcher("Donald"), |
|||
new ChildMovieWatcher("Lisa"), |
|||
new YoungChildMovieWatcher("Fred") ); |
|||
for ( MovieWatcher movieWatcher : movieWatchers ) { |
|||
movieWatcher.watchMovie(); |
|||
movieWatcher.eatPopcorn(); |
|||
} |
|||
} |
|||
} |
|||
// Base class for MovieWatcher's |
|||
class MovieWatcher { |
|||
public MovieWatcher(String aName) { |
|||
name = aName; |
|||
} |
|||
public void watchMovie() { |
|||
System.out.println(name + " is watching the movie"); |
|||
} |
|||
public void eatPopcorn() { |
|||
System.out.println(name + " is eating popcorn"); |
|||
} |
|||
private final String name; |
|||
} |
|||
// ParentMovieWatcher cannot be extended because it is 'final' |
|||
final class ParentMovieWatcher extends MovieWatcher { |
|||
public ParentMovieWatcher(String aName) { |
|||
super(aName); |
|||
} |
|||
} |
|||
// ChildMovieWatcher can be extended. |
|||
class ChildMovieWatcher extends MovieWatcher { |
|||
public ChildMovieWatcher(String aName) { |
|||
super(aName); |
|||
name = aName; |
|||
} |
|||
// The method eatPopcorn() cannot be overridden because it is 'final' |
|||
public final void eatPopcorn() { |
|||
System.out.println(name + " is eating too much popcorn"); |
|||
} |
|||
private String name; |
|||
} |
|||
class YoungChildMovieWatcher extends ChildMovieWatcher { |
|||
public YoungChildMovieWatcher(String aName) { |
|||
super(aName); |
|||
name = aName; |
|||
} |
|||
// The method watchMovie() cannot be overridden because it is 'final' |
|||
public final void watchMovie() { |
|||
System.out.println(name + ", you are too young to watch the movie."); |
|||
} |
|||
private String name; |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Donald is watching the movie |
|||
Donald is eating popcorn |
|||
Lisa is watching the movie |
|||
Lisa is eating too much popcorn |
|||
Fred, you are too young to watch the movie. |
|||
Fred is eating too much popcorn |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |