Questions tagged [sealed]

The sealed modifier keyword prevents a C# class from being inherited. Similar to the final keyword in Java.

The sealed modifier keyword prevents a C# class from being inherited. Similar to the final keyword in Java.

183 questions
373
votes
6 answers

What is a sealed trait?

Sealed classes are described in 'Programming in Scala', but sealed traits are not. Where can I find more information about a sealed trait? I would like to know, if a sealed trait is the same as a sealed class? Or, if not, what are the differences?…
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
164
votes
7 answers

Static and Sealed class differences

Is there any class that be implemented in static class? means: static class ABC : Anyclass Is there any class which can be inherited in both sealed class and static class? means: static class ABC : AClass {} And sealed class ABC : AClass…
Saurabh Mahajan
  • 2,937
  • 7
  • 25
  • 32
74
votes
2 answers

Scala's sealed abstract vs abstract class

What is the difference between sealed abstract and abstract Scala class?
Łukasz Lew
  • 48,526
  • 41
  • 139
  • 208
73
votes
6 answers

What is an internal sealed class in C#?

I was looking through some C# code for extending language support in VS2010 (Ook example). I saw some classes called internal sealed class What do these do? Would one use them?
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
61
votes
6 answers

How to define sealed class in C++?

How to stop the class to be inherited by other class.
Shashi
  • 2,860
  • 2
  • 34
  • 45
59
votes
3 answers

Is there an equivalent to "sealed" or "final" in TypeScript?

I'm trying to implement a method in a super class that should be available for use, but not changeable, in sub classes. Consider this: export abstract class BaseClass { universalBehavior(): void { doStuff(); // Do some universal stuff…
bubbleking
  • 3,329
  • 3
  • 29
  • 49
56
votes
4 answers

Should I seal all classes I know shouldn't ever be used as a base class?

Should I seal all classes I know shouldn't ever be used as a base class even when there are no tangible performance or security concerns, or is this just adding cruft?
Daniel Coffman
  • 1,997
  • 3
  • 26
  • 34
42
votes
6 answers

Sealed method in C#

I am a newbie in C#.I am reading about Sealed keyword.I have got about sealed class.I have read a line about Sealed method where we can make Sealed method also.The line was (By declaring method as sealed, we can avoid further overriding of this…
Mohit Kumar
  • 1,885
  • 5
  • 21
  • 24
41
votes
6 answers

Iteration over a sealed trait in Scala?

I just wanted to know if it is possible to iterate over a sealed trait in Scala? If not, why is it not possible? Since the trait is sealed it should be possible no? What I want to do is something like that: sealed trait ResizedImageKey { /** *…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
38
votes
9 answers

Why aren't classes sealed by default?

I was just wondering, since the sealed keyword's existence indicates that it's the class author's decision as to whether other classes are allowed to inherit from it, why aren't classes sealed by default, with some keyword to mark them explicitly as…
xyz
  • 27,223
  • 29
  • 105
  • 125
38
votes
4 answers

Should IEquatable, IComparable be implemented on non-sealed classes?

Anyone have any opinions on whether or not IEquatable or IComparable should generally require that T is sealed (if it's a class)? This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of…
Phil
  • 2,675
  • 1
  • 25
  • 27
30
votes
6 answers

What is the difference between a class having private constructor and a sealed class having private constructor?

Is there any difference between A and B? Class A has private constructor: class A { private A() { } } Class B is sealed and has a private constructor: sealed class B { private B() { } }
dvjanm
  • 2,351
  • 1
  • 28
  • 42
28
votes
3 answers

Sealed keyword in association with override

Is it always necessary to follow the sealed keyword with override in the signature of a method like the below code: public sealed override string Method1(){.....} I mean, if I want to "seal" the method within the base class without overriding, is…
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
27
votes
4 answers

Preventing a method from being overridden in C#

How do I prevent a method from being overridden in a derived class? In Java I could do this by using the final modifier on the method I wish to prevent from being overridden. How do I achieve the same in C#? I am aware of using sealed but apparently…
nighthawk457
  • 1,102
  • 3
  • 12
  • 27
25
votes
8 answers

Why are interfaces not able to be marked as sealed?

public sealed interface IMyInterface { } Gives "The modified 'sealed' is not valid for this item" I can understand in some ways that an interface must be descendable otherwise the class cannot implement it. But why can I not specify that an…
weston
  • 54,145
  • 21
  • 145
  • 203
1
2 3
12 13