473,569 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sealed method question.

Hi,

I am curious about how to write C# class with this requirement : I want to
make one of the methods of the class to be non overridable in derived
classes.
I cannot use sealed because sealed keyword on a method should also have
override keyword. My class here is the master class.

Consider a class Master with 2 methods

public class Master
{
public virtual string Method1()
{
}
public string Method2()
{
}
}

public class Child : Master
{
public override string Method1()
{
}
}

how to make Method2 as non-overridable and Method1 as overridable by Child
class?
Nov 16 '05 #1
3 6464
Methods can not be overridden unless they are virtual or abstract or
overridden
from a lower class.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"avnrao" <av*@newsgroups .com> wrote in message
news:up******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I am curious about how to write C# class with this requirement : I want to
make one of the methods of the class to be non overridable in derived
classes.
I cannot use sealed because sealed keyword on a method should also have
override keyword. My class here is the master class.

Consider a class Master with 2 methods

public class Master
{
public virtual string Method1()
{
}
public string Method2()
{
}
}

public class Child : Master
{
public override string Method1()
{
}
}

how to make Method2 as non-overridable and Method1 as overridable by Child
class?

Nov 16 '05 #2
As Dennis say, you don't have to anything special to make a method non-overridable as that is the default in C# (unlike Java). This means that a base class developer has to think explicitly about how they want derived classes to interact with their code and make the methods that want to be replacable virtual (or abstract).

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Methods can not be overridden unless they are virtual or abstract or
overridden
from a lower class.
Nov 16 '05 #3
your code is good.

Method1 is non-overridable (since it is not virtual). The only way for its
child classes to define their own Method1 is if it gets shadowed or newed.

public new string Method1()
"avnrao" <av*@newsgroups .com> wrote in message
news:up******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I am curious about how to write C# class with this requirement : I want to
make one of the methods of the class to be non overridable in derived
classes.
I cannot use sealed because sealed keyword on a method should also have
override keyword. My class here is the master class.

Consider a class Master with 2 methods

public class Master
{
public virtual string Method1()
{
}
public string Method2()
{
}
}

public class Child : Master
{
public override string Method1()
{
}
}

how to make Method2 as non-overridable and Method1 as overridable by Child
class?

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2919
by: Zeng | last post by:
Would somebody know when we should seal a class? Shouldn't all classes be open up for inheritance? Thanks!
4
12866
by: Néstor Marcel Sánchez Ahumada | last post by:
In a method declaration the 'sealed' keyword must be used with the 'override' keyword to avoid further overriding. Thus it can't be used in base classes. Why? This would be a good enhancement for C# 3.0? Any comments? Néstor
13
8091
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that the code inside the constructor / desconstructor will never run? Mark
9
8400
by: Kylin | last post by:
any better reason ? -- FireCrow Studio Kylin Garden EMail:gaotianpu@gmail.com ICQ:156134382
10
3853
by: TJM | last post by:
Hi, Is it possible to have a method sealed and abstract at the same time? MSDN states clearly that this is not allowed for classes but it does not mention it for methods. I tried with a simple example and the compiler would not allow me to compile, however in a recent interview, I was asked this question and the interviewer claimed that...
9
3973
by: lee.chappers | last post by:
How can I make the CodeDom generate the following C# method? protected sealed override void Test() { } I've tried using: domMethod.Attributes = MemberAttributes.Override | MemberAttributes.Family | MemberAttributes.Final;
18
2941
by: Vedo | last post by:
ref struct XXX abstract sealed { literal int A = 5; }; The definition above gives me the compiler warning "C4693: a sealed abstract class cannot have any instance members 'A'". The equivalent definition is perfectly legal in other CLR languages. For example in C#; static class XXX
1
1825
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks, Mulugeta
1
1839
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks, Mulugeta
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.