473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Is it possible to hide a method from inheriting from a parent class ?

Suppose I have a class

Expand|Select|Wrap|Line Numbers
  1. class A
  2.   {
  3.     public void Foo()
  4.     {
  5.  
  6.      }
  7. }
i want to inherit A to class B & C

Expand|Select|Wrap|Line Numbers
  1. class B:A
  2.   {
  3.  
  4.   }
  5.  
  6. class C:A
  7. {
  8.  
  9. }
How can I write the class A so that I can access Foo() by object of class B but by C object Foo() will be unavailable.Is it possible?.
Dec 28 '09 #1
5 2410
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Dec 28 '09 #2
GaryTexmo
1,501 Expert 1GB
I don't think you can do what you want. I think the best you can hope for is to make the class a sealed class so that it can't be inherited. Here, have a look at this link, hopefully it explains things better than I can, inheritance has never been a strong suit of mine.

http://www.csharp-station.com/tutorials/lesson19.aspx
Dec 29 '09 #3
Dear Gary,
Thank you so much for your kind respone.
Dec 30 '09 #4
Plater
7,872 Expert 4TB
Quote MSDN:
The sealed modifier can be applied to classes, instance methods and properties. A sealed class cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class. When applied to a method or property, the sealed modifier must always be used with override (C# Reference).
Says you can seal an individual method, which might work for you.


Your other option might be to mark things internal. Which means only classes in the same assembly can use it. So if you are say compiling a DLL with a base class and a class deriving from it. Only the deriving classes in your DLL can use the function. At least that's how I've come to understand it.
Dec 30 '09 #5
GaryTexmo
1,501 Expert 1GB
This is why I love this forum, you learn something new almost every day. Nice work, Plater! :)
Dec 30 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steve | last post by:
Visual Studio 2003 C# Windows: I have a tree view control as my main menu control down the left side of my application. This has 2 Parent Nodes on it (Jobs and Employees). beneath these 2 main...
5
by: Zambien | last post by:
Hi all, Here's my problem. I have tables that are using the menu/submenu idea for hiding rows. This works fine in IE (of course) and does show/hide correctly in netscape, but as soon as the...
5
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
3
by: Tom Jones | last post by:
I do not understand what is meant when someone states that a given method is "hidden" verses overriden. Would someone please provide a short example of both cases and why you might want to...
1
by: Peter J. Bismuti | last post by:
How do you access attributes of a class when inheriting from it? Can't you just say: self.attribute? Help?!...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
2
by: Jack | last post by:
Hello, I have a 3-level class hirarchy: 1=Grandparent 2=Parent 3=Child The Grandparent has a method called OnPaint which I want to override from the Child class. I can do this, but how...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.