Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on inheritence in c# within the C# forums, part of the Software Development category; what is 1.virtual 2.override 3.sealed override 4.new...........
|
|||||||
|
|||
|
Re: inheritence in c#
All these keywords are used with Methods in class.
1.Virtual: - if you use these keyword with method, the method may be overridden in derived class, not compulsory. class MyClass { public virtual void MyMethod() {..........} } 2.Override: - To override a method in derived class this keyword is used. To use this keyword in derived class the method in base class must be declared either with virtual or abstract. class MyClass { public virtual void MyMethod() {..........} } class childClass:MyClass { public override void MyMethod() {..........} } 3.Sealed Override: - This keyword must be used with a method in derived class only, to avoid overriding in further derived class of this class.To use this keyword in derived class the method in base class must be declared either with override or abstract.if you use this keyword the method can't overriden in further derived class. class MyClass { public virtual void MyMethod() {..........} } class childClass:MyClass { public sealed override void MyMethod() {..........} } next if you derive class from this MyClass you can't override MyMethod 4.New - this keyword can be used with any methods to write new definition in derived class, to hide base class methods that is not declared with the keyword virtual or abstract. class MyClass { public void MyMethod() {..........} } class childClass:MyClass { public new void MyMethod() {..........} } Last edited by shilpa.c; 08-25-2009 at 04:06 AM. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|