C# sealed vs Java final - c#

Would anybody please tell me as the reason the following use of sealed does not compile? Whereas, if I replace sealed with final and compile it as Java, it works.
private sealed int compInt = 100;
public bool check(int someInt)
{
if (someInt > compInt)
{
return true;
}
return false;
}

That's because final in Java means plenty of different things depending on where you use it whereas sealed in C# applies only to classes and inherited virtual members (methods, properties, events).
In Java final can be applied to:
classes, which means that the class cannot be inherited. This is the equivalent of C#'s sealed.
methods, which means that the method cannot be overridden in a derived class. This is the default in C#, unless you declare a method as virtual and in a derived class this can be prevented for further derived classes with sealed again. That's why you see sealed members in C# a lot less than final members in Java.
fields and variables, which means that they can only be initialized once. For fields the equivalent in C# is readonly.

Sealed in C# can be applied only to a reference types, and has impact on inheritance tree.
In practise the type marked as sealed guranteed to be the last "leaf" in the inheritance tree, or in short, you can not derive from the type declared like a sealed.
public sealed class Child : Base
{
}
public class AnotherAgain : Child //THIS IS NOT ALLOWED
{
}
It can not be applied to a members.

Tigran's answer is not wrong while Joey's is a little incorrect.
Firstly you can look into this page: What is the equivalent of Java's final in C#?.
the sealed key word can apply to class,instance method and property but not variables, or interface's methods. Classes with sealed cannot be inherited. When sealed put on method, it must be by override in company. Every struct is sealed, so struct cannot be inherited. Check this image:

Related

How to have all my classes default functionalities like .ToString method

I want to have all my classes some set of behaviour like all classes in .net (ToString, GetHashCode etc.) have.
But I don't want to create a base class which have these type of functions and inherit all the classes from this base class. By going this way I will lost the liberty of inherting my classes from any other class (since .net support inheritance from only one class).
How .net framework create a class without inherting from base object class but has virtual behaivour in all classes?
We don't write like this
class MyClass : System.Object
{
}
but MyClass gets virtual functions of System.Object.
You do not have to explicitly declare that your class inherits from System.Object because the compiler will enforce that your class derive from System.Object automatically if you do not want to do so manually for it could become very tedious.
You can confirm this yourself by declaring a class in your code and then disassembling the assembly output by the compiler. I declared a class class Person { } and disassembled the output. The following IL was produced
.class public auto ansi beforefieldinit Code.Person
extends [mscorlib]System.Object
If you want to define some common functionality amongst your classes without a base class then you might consider writing an extension method on System.Object
public static class ExtensionMethods
{
public static void DoSomething(this object target)
{
}
}
You could be more explicit yet and define an interface that your classes could implement and then define the extension method for said interface. Because there are no limitiations to how many interfaces you can implement this might mitigate your concerns about multiple inheritance.
To build on ByteBlast's post and address harpo's concern, you could use decorator interfaces with extension methods.
public interface IMyDecorator{}
public interface IMySecondDecorator : IMyDecorator {}
public static class ExtensionMethods
{
public static void Print(this IMyDecorator target)
{
}
public static void Print(this IMySecondDecorator target)
{
}
}
Perhaps what you want to have done can be accomplished with PostSharp? Essentially have the tool replace all classes which inherit from System.Object with an inheritance from your custom class?
It's an interesting question, but I think the answer is, you can't. If you're not willing to use a universal base class, then you cannot provide universal behavior for methods inherited from object.
If this really matters to you, then it's worth considering the base class route. Of course, you can't make it apply to framework classes, but those are sealed (or invisible) in many cases anyway.
I have been thinking about this question because I'm working with a few classes that do nothing but provide GetHashCode and Equals overrides for classes with value-type semantics. In several cases, it would be very handy to use an alternate base class, but you simply cannot override those behaviors by any other means (e.g. interfaces/extension methods).
A universal base class is the obvious answer to this problem but will not provide the 'standard' implementation for classes that inherit from types outside of your application's class hierarchy.
I would consider composition in place of inheritance. This is the essence of what has been proposed by #ByteBlast and #PhilipScottGivens.
Why not have a helper class that provides the functionality for you GetHashCode and ToString methods (I am picturing some reflection in both of these so that you can work with the members / properties of the instances of your types) and whatever other common services you require for all objects?
An instance of this (or maybe the helper has static methods that you pass an instance to - much like the extension methods) is passed into each object or created by the instance of your object.

Implementing the Template Method pattern in C#

The template method pattern provides that the abstract base class has a not overridable method: this method implements the common algorithm and should not overridden in the subclasses. In Java the template method is declared final within the abstract base class, in C# the sealed keyword has a similar meaning, but a not overridden method can not be declared sealed.
public abstract class Base
{
protected abstract AlgorithmStep1();
protected abstract AlgorithmStep2();
public sealed void TemplateMethod() // sealed: compile error
{
AlgorithmStep1();
AlgorithmStep2();
}
}
How can I solve this problem?
Why can not prevent a method can be overridden by subclasses (in C#)?
The sealed modifier is only valid for function members which are overriding base class members, to stop them from being virtual for derived classes. Function members are non-virtual by default in C# (unlike Java). You still need the sealed modifier for a class though - classes aren't sealed by default.
Just remove the sealed modifier from your method and it should be fine.
See section 10.6.5 of the C# 4 spec for more details about sealed methods (sealed properties and events are in section 10.7.5 and 10.8.4 respectively).
When an instance method declaration includes a sealed modifier, that method is said to be a sealed method. If an instance method declaration includes the sealed modifier, it must also include the override modifier. Use of the sealed modifier prevents a derived class from further overriding the method.
Just remove the sealed keyword. By default, methods are not overridable; subclasses cannot override them, only hide them.
C# methods are sealed by default

How to define a must inherit class

How to define a must inherit class? in C#
You mark the class as abstract (this is the C# analogue to the VB.NET Must Inherit).
This will ensure it can't be instantiated directly.
From the linked MSDN article:
The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.
(emphasis mine)
Use the abstract modifier.
public abstract class MyClass()
{
...
}
You can define a class as abstract, or give it a protected-only constructor. abstract is better.
If u want to create a class, that has to be inherited, you'll need to mark it with the abstract modifier.
public abstract MyClass
{
}
It's not possible enforse needness of derivation or implementation in code, if that was a question.
But:
You can define an interface to force consumer to implement it.
Or you can define abstract class with only abstract members to force consumer to override all of them.
Hope this helps.
An interface would be best.
If you need to simulate the functionality , and its not a requirement that it fail at compile time...
define a method in the base class. Throw a an exception as the only line in the implementation. You might want to make the message very very clear about what the problem is.
override the method in the super class(es) and implement them.
If you fail to implement in a super class, you will get the exception.
Not perfect, but say you are trying to port code from vb.net... this could work.

c# abstract methods: internally public and virtual?

Are abstract methods internally public and virtual in c#?
All methods are, by default, private and if an abstract method is private, it will not be available to derived class, yielding the error "virtual or abstract members cannot be private"
I think you are asking a different question than most people think (in other words it seems like you understand what abstract means).
You cannot declare a private abstract method - the compiler issues an error. Both of these classes will not compile:
class Foo
{
private abstract void Bar();
}
class Baz
{
// This one is implicitly private - just like any other
// method declared without an access modifier
abstract void Bah();
}
The compiler is preventing you from declaring a useless method since a private abstract member cannot be used in a derived class and has no implementation (and therefore no use) to the declaring class.
It is important to note that the default access modifier applied to an abstract member by the compiler (if you do not specify one yourself) is still private just like it would be if the method was not abstract.
Abstract is just a way to say: "I am here, but no one has told me what I'm going to do yet." And since no one has implemented that member yet someone must do that. To do that you have to inherit that class, and override that member.
To be able to override something it has to be declared either abstract or virtual, and must at least be accessible to the inheritor, i.e. must be marked protected, internal or public.
Abstract methods cannot be private and are virtual. They must be at least protected.
By virtue of Jon Skeet's argument here (What are the Default Access Modifiers in C#?)
The default access for everything in C# is "the most restricted access you could declare for that member"
It must be "protected"
As pointed out by Pieter default is always private, so:
abstract class Foo
{
abstract void Bar();
}
Gives compiler error
virtual or abstract members cannot be private

Why is the sealed keyword not included in the list of access modifiers?

I think sealed should be included in the list of access modifiers in C# language. Can somebody tell the reason why it has been excluded?
It's not an access modifier, it's to do with whether a class can be inherited from or not...
An access modifier defines who can access the method or class, and when (i.e.: private: only class members, public: everyone else etc). Marking a method or a class as sealed means that it cannot be inherited. It says nothing about access per se.
Properly said: you still need to add an access modifier if you use the sealed keyword (unless the default access modifier suits you).
Your confusion may be about that both keywords seem to be about protection levels. This is kind of true, but we don't have a notion of protection modifier. The sealed keyword is called the sealed modifier, because it modifies a class or method to be sealed off. This is like a boolean switch: a class or method is either sealed or it is not, regardless of its access modifiers.
To add to the confusion, there exist sealed accessors, which means that derivation of an accessor (gettor/settor) is not allowed (C# standard 10.7.5).
Cause if you cannot derive from a class it doesn't mean you cannot access it.
All the following valid class definitions feature sealed classes but they all have different levels of access, so you can see that sealed isn't an access modifier and hence isn't listed as one by Microsoft:
public sealed class MyPublicClass
{
}
internal sealed class MyInternalClass
{
}
private sealed class MyPrivateClass
{
}
You have to trust that Microsoft do know a thing or two about the language they created :)

Categories

Resources