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?

Was it helpful?

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

  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.

  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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top