C# class/methods modifiers
Question
I'm trying to make spreadsheet with class/method modifiers. The spreadsheet itself can be located here, although I have some questions:
1) As I read, methods also can be sealed
, but what's the purpose of this? Protecting method against overriding?
2) Does fields also have to be abstract
in abstract
class?
3) Can virtual
method have body in defined object/class?
Solution
1: yes, exactly that; stopping a virtual method from being overridden again in a subclass
2: fields are never abstract; fields are implementation; nothing has to be abstract in an abstract class
3: yes, in that the virtual modifier is distinct from the abstract modifier. Both represent a virtual-method - the difference is entirely whether there is an implementation in the base class
OTHER TIPS
sealed
methods prevents further overriding down the inheritance chain.sealed
methods also need to have override keyword.Fields cannot be
abstract
. Field is a class level variable. You can't override that. Properties and methods can beabstract
, but doesn't need to.The difference between
virtual
andabstract
methods is that virtual method has an implementation. You can override that implementation in derived class.
- Yes
- No - there is no such thing as abstract field, the class is abstract, meaning it cannot be instantiated but the fields defined on it are part of the layout of derived non-abstract classes.
- Yes