1

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?

Darj
  • 1,403
  • 1
  • 17
  • 47
  • 1
    you can try them all and see what happen – Sleiman Jneidi Jun 24 '12 at 11:51
  • 2) A field declaration reserves storage without opportunity to define or redefine an interaction. Overrides occur on interactions, like methods and property getters/setters. It may seem unclear at first that you can override a property, when a property can appear to be storage. If so, remember this is simply a nicety of C#; that when you declare myProp{get;set;}, it automatically creates a backing field for you, and the interaction occurs against that. – shannon Jun 24 '12 at 12:09

3 Answers3

3
  1. sealed methods prevents further overriding down the inheritance chain. sealed methods also need to have override keyword.

  2. Fields cannot be abstract. Field is a class level variable. You can't override that. Properties and methods can be abstract, but doesn't need to.

  3. The difference between virtual and abstract methods is that virtual method has an implementation. You can override that implementation in derived class.

Community
  • 1
  • 1
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
3

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

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1
  1. Yes
  2. 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.
  3. Yes
Ventsyslav Raikov
  • 6,882
  • 1
  • 25
  • 28