Sealed classes and methods: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Phix}}: added "more sensible way")
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(13 intermediate revisions by 6 users not shown)
Line 6:
Classes are sometimes made non-subclasssable 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 'public' methods for the same reasons and with the same potential benefits. 'private' methods should be effectively sealed anyway.
 
;Task
Line 21:
* [[Inheritance/Single]]
* [[Inheritance/Multiple]]
 
=={{header|C}}==
C isn't an object oriented language though it can and has been used to create other languages which are.
 
It has structs rather than classes which are just a collection of fields. Methods on a struct can be simulated by functions whose first argument is a pointer to the struct.
 
To simulate inheritance, one can embed a 'parent' field ''first'' in the 'child' struct and then pass the address of that field to 'parent' methods.
 
However, there is no way that the method can tell whether it's receiving a pointer to a 'parent' instance or a pointer to a 'child' field. To use the Wren technique for simulating sealed methods, we therefore need to pass a further parameter, namely a type identifier, as the following code illustrates.
<syntaxhighlight lang="c">#include <stdio.h>
 
typedef enum {
PARENT,
CHILD
} typeid;
 
typedef struct {
const char* name;
int age;
} parent;
 
typedef struct {
parent p;
} child;
 
void watchMovie(parent *p, typeid id) {
if (id == CHILD && p->age < 15) {
printf("Sorry, %s, you are too young to watch the movie.\n", p->name);
} else {
printf("%s is watching the movie...\n", p->name);
}
}
int main() {
parent p = { "Donald", 42 };
child c1 = { "Lisa", 18 };
child c2 = { "Fred", 10 };
watchMovie(&p, PARENT);
watchMovie(&c1.p, CHILD);
watchMovie(&c2.p, CHILD);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Donald is watching the movie...
Lisa is watching the movie...
Sorry, Fred, you are too young to watch the movie.
</pre>
 
=={{header|C++}}==
Classes and functions can be sealed in C++ by using the '''final''' keyword.
<syntaxhighlight lang="cpp">#include <iostream>
#include <memory>
#include <string>
#include <vector>
 
class MovieWatcher // A base class for movie watchers
{
protected:
std::string m_name;
 
public:
explicit MovieWatcher(std::string_view name) : m_name{name}{}
virtual void WatchMovie()
{
std::cout << m_name << " is watching the movie\n";
}
virtual void EatPopcorn()
{
std::cout << m_name << " is enjoying the popcorn\n";
}
virtual ~MovieWatcher() = default;
};
 
// ParentMovieWatcher cannot be inherited from because it is 'final'
class ParentMovieWatcher final : public MovieWatcher
{
public:
explicit ParentMovieWatcher(std::string_view name) : MovieWatcher{name} {}
};
 
// ChildMovieWatcher can be inherited from
class ChildMovieWatcher : public MovieWatcher
{
public:
explicit ChildMovieWatcher(std::string_view name)
: MovieWatcher{name}{}
// EatPopcorn() cannot be overridden because it is 'final'
void EatPopcorn() final override
{
std::cout << m_name << " is eating too much popcorn\n";
}
};
 
class YoungChildMovieWatcher : public ChildMovieWatcher
{
public:
explicit YoungChildMovieWatcher(std::string_view name)
: ChildMovieWatcher{name}{}
// WatchMovie() cannot be overridden because it is 'final'
void WatchMovie() final override
{
std::cout << "Sorry, " << m_name <<
", you are too young to watch the movie.\n";
}
};
 
int main()
{
// A container for the MovieWatcher base class objects
std::vector<std::unique_ptr<MovieWatcher>> movieWatchers;
// Add some movie wathcers
movieWatchers.emplace_back(new ParentMovieWatcher("Donald"));
movieWatchers.emplace_back(new ChildMovieWatcher("Lisa"));
movieWatchers.emplace_back(new YoungChildMovieWatcher("Fred"));
 
// Send them to the movies
std::for_each(movieWatchers.begin(), movieWatchers.end(), [](auto& watcher)
{
watcher->WatchMovie();
});
std::for_each(movieWatchers.begin(), movieWatchers.end(), [](auto& watcher)
{
watcher->EatPopcorn();
});
}
</syntaxhighlight>
{{out}}
<pre>
Donald is watching the movie
Lisa is watching the movie
Sorry, Fred, you are too young to watch the movie.
Donald is enjoying the popcorn
Lisa is eating too much popcorn
Fred is eating too much popcorn
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vb">Type Parent
nombre As ZString * 7
edad As Byte
Declare Operator Cast () As String
End Type
 
Operator Parent.cast As String
Return this.nombre & " is watching the movie..."
End Operator
 
Type Child Extends Parent
Declare Operator Cast As String
End Type
 
Operator Child.cast As String
If this.edad < 15 Then
Return "Sorry, " & this.nombre & ", you are too young to watch the movie."
Else
Return this.nombre & " is watching the movie..."
End If
End Operator
 
Dim As Parent p1, p2
p1.nombre = "Donald" : p1.edad = 42
p2.nombre = "Dougal" : p2.edad = 12
Print p1
Print p2
 
Dim As Child c1, c2
c1.nombre = "Lisa" : c1.edad = 18
c2.nombre = "Fred" : c2.edad = 10
Print c1
Print c2
 
Sleep</syntaxhighlight>
{{out}}
<pre>Donald is watching the movie...
Dougal is watching the movie...
Lisa is watching the movie...
Sorry, Fred, you are too young to watch the movie.</pre>
 
=={{header|Go}}==
Go isn't really an object oriented language - not in a conventional sense anyway.
 
It has structs rather than classes which are just a collection of fields. It does however have methods which are declared outside the struct itself but within the same package and whose receiver is either a struct instance or a pointer to one. Non-struct types can also have methods though this isn't relevant here.
 
As a general rule, all entities are accessible within the same package but are only accessible to other packages if their name begins with an upper case letter.
 
Inheritance is not supported but can be simulated to some extent by embedding one struct inside another. The latter is then able to access the former's fields directly and to call its methods.
 
Consequently, a Go struct and its methods are effectively sealed unless the struct is embedded in another one. However, the only way to prevent embedding from outside the package would be to make the struct private to its package which may not be an acceptable solution unless it and/or its methods could be exposed indirectly.
 
Fortunately, as the following example shows, the Wren technique for sealing methods can still be used provided we pass a further parameter (a type identifier) to the method so that it knows whether its being called with a pointer to a 'parent' or to a 'child' instance. This information is needed because the type system is such that the runtime type of the receiver will always be 'parent'.
<syntaxhighlight lang="go">package main
 
import "fmt"
 
type typeid int
 
const (
PARENT typeid = iota
CHILD
)
 
type parent struct {
name string
age int
}
 
type child struct {
parent // embedded struct
}
 
func (p *parent) watchMovie(id typeid) {
if id == CHILD && p.age < 15 {
fmt.Printf("Sorry, %s, you are too young to watch the movie.\n", p.name)
} else {
fmt.Printf("%s is watching the movie...\n", p.name)
}
}
 
func main() {
p := &parent{"Donald", 42}
p.watchMovie(PARENT)
c1 := &child{parent{"Lisa", 18}}
c2 := &child{parent{"Fred", 10}}
c1.watchMovie(CHILD)
c2.watchMovie(CHILD)
}</syntaxhighlight>
 
{{out}}
<pre>
Donald is watching the movie...
Lisa is watching the movie...
Sorry, Fred, you are too young to watch the movie.
 
</pre>
 
=={{header|J}}==
No J compilers have been released (though some people have claimed to be working on such things). That said, J does provide a locked script mechanism which might be thought of as compiled code (which depends on libj).
 
J, by default, does not support sealed classes (nor methods). However, sealed classes could be implemented by altering J's <code>coinsert</code> (which implements inheritance) to omit sealed classes.
 
For example, we could say that a class which contained any implementation of <code>final</code> is a sealed class:
 
<syntaxhighlight lang=J>coinsert=: {{
l=. (#~{{0>nc<'final__y'}}"0);: :: ] y
p=. ; (, 18!:2) @ < each l
p=. ~. (18!:2 coname''), p
(p /: p = <,'z') 18!:2 coname''
}}</syntaxhighlight>
 
J does not provide a mechanism to seal individual methods.
 
=={{header|Julia}}==
Julia's multiple dispatch and type system sit firmly outside the context within which sealed (non-inheritable) classes make sense.
 
First, within Julia's class type system, all inheritance is between abstract types. Objects can have inheritance from abstract types, but objects cannot inherit from other objects. Thus, in Julia, all concrete objects are final.
 
Second, because of Julia's multiple dispatch, all object methods (except certain constructors, called inner constructors) can be overloaded within user code. Thus, only inner construction methods are final methods.
 
Thus, Julia both enforces a kind of sealed classes for inheritance with all of its objects (the first above) and yet prevents any simple sealing for those same objects' methods (the second above).
 
=={{header|Nim}}==
Nim allows to define object types. There is two ways to make a type inheritable. Here is an example:
 
<syntaxhighlight lang="Nim">type T1 = object # Non inheritable.
type T2 = object of RootObj # Inherits from Root and is inheritable.
type T3 {.inheritable.} = object # New object root which is inheritable.
 
type T4 = object of T2 # Inheritable.
type T5 = object of T3 # Inheritable.
type T6 {.final.} = object of T2 # Non inheritable.
</syntaxhighlight>
 
As we can see in the example, if a type inherits from another type, it is also inheritable unless it is annotated with the <code>final</code> pragma.
 
In Nim, one can define methods on objects. There is no way to declare a method as final to forbid its overriding.
 
=={{header|Phix}}==
Line 102 ⟶ 383:
 
3. Using the 'is' operator (i.e. a ''is'' C) to detect the type of 'a' wouldn't work here as this would return 'true' if 'a' were either a 'C' object or an object of a subclass of 'C'. It is possible to spoof the 'is' operator by overriding its normal behavior, though this is definitely not recommended!
<syntaxhighlight lang="ecmascriptwren">class Parent {
construct new(name, age) {
_name = name
9,476

edits