Skip to content

19. Override/New/Shadowing, Virtual/Sealed, Component

February 27, 2011

55. Juxtapose the use of override with new? What is shadowing?
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer or event. In this example the square class must provide an overriden implementation of Area because Area is inherited from the abstract ShapesClass:
Override Sample From abstract base class with abstract method to implementation using virtual in derived class

When new is used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. New documents the fact the derived version is intended as a replacement. To hide an inherited member, declare it in the derived class by using the same name, and modify it with the new modifier. For example:
New keyword used as modifier to hide member from base class
A derived type shadows the name of an inherited type member by redeclaring it. Shadowing a name does not remove the inherited type members with that name, it merely makes all of the inherited type members with that name unavailable in the derived class. The shadowing declaration may be any type of entity. In other words Shadowing is another name for disabling an inherited method and giving it a new definition.

56. Explain the use of virtual, sealed, override and abstract.
The virtual keyword is used to modify a method, property, or event declaration and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:
Virtual Method that can be overridden in derived class
When applied to a class, the sealed modifier prevents other classes from inheriting from it. In the following example, class B inherits from class A, but no class can inherit from class B.
Class cannot inherit from a sealed class
You can also use the sealed modifier on a method or a property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.
Sealed method override sample

Override redefines a method declared as virtual. Abstract defines a class that cannot be instantiated, or a method that must be overridden in a derived class.

57. Explain the importance and use of each component in this string: Foo.Bar, Version=, Culture=neutral, PublicKeyToken=593777ae2d274679d
Foo.Bar is the Assembly Name which is used for loading. Version= is the version of the assembly, also used for loading. Culture=neutral defines culture settings used for string translations and other locale specific settings. PublicKeyToken=593777ae2d274679d is used to uniquely identify the assembly and prevent collisions with other assemblies.

Happy Programming! =)

Source: mainly MSDN.


From → OOP

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: