473,395 Members | 1,761 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,395 software developers and data experts.

How to change public properties in base class as private in derived class.

Hi all,

I have these classes

public class Base
{
private string str;
private string str1;
public string MyStr;
{
get { return str; }
}
public string MySecondStr
{
get { return str1; }
}
}

public class Derived: Base
{
}

What I need is while creating a instance to Derived class, I need to
access only MySecondStr property and I don't want MyStr property. Is it
possible to restrict the MyStr property when accessing the derived
class object. Could anyone help me how to do this?

Thanks in advance

Mar 21 '06 #1
4 1483
"santel" <sa***********@sify.com> a écrit dans le message de news:
11**********************@z34g2000cwc.googlegroups. com...

| I have these classes
|
| public class Base
| {
| private string str;
| private string str1;
| public string MyStr;
| {
| get { return str; }
| }
| public string MySecondStr
| {
| get { return str1; }
| }
| }
|
| public class Derived: Base
| {
| }
|
| What I need is while creating a instance to Derived class, I need to
| access only MySecondStr property and I don't want MyStr property. Is it
| possible to restrict the MyStr property when accessing the derived
| class object. Could anyone help me how to do this?

No, this is not possible. It violates the first principles of OO design. If
you make anything public in the base class, then it has to be public in
derived classes; a derived class is *everything* that the base class is. You
need to rethink your design :-(

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 21 '06 #2
Thanks for your reply.

Mar 21 '06 #3
Thanks for your reply.

Mar 21 '06 #4
santel,

Joanna is right. You cannot do that. Even if you could nothing stops the
code using objects of your dericed class to cast them to the base class and
use the public property there.

The best you can do I believe is to declare MyStr proprety virtual, override
it in the dericed class and throw some unsopported-property exception there.

Anyways if you feel the need to hide some member that probaly means your
dericed class is not your base class and shouldn't derive from it. I also
agree that sometimes if doesn't make sense to introduce a whole new branch
in the hierarchy just for a class that doesn fit because of one property .
In the latter case exceptions and good documentation might be better
solution.

Again review your design it might turn out that the new class doesn't have
to inherit from the base one or that you need to add one more level in the
hierarchy.
--
HTH
Stoitcho Goutsev (100)
"santel" <sa***********@sify.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Thanks for your reply.

Mar 21 '06 #5

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

Similar topics

2
by: ET2 Ng | last post by:
Hi all, If I recall correctly, the functions inherited from a base class are private in the derived class. I want to expose some of them (but not all of them) directly through my derived...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
7
by: Peter Oliphant | last post by:
I use to make the statement that you could always replace any 'private' or 'public' access with 'public' and the code would still compile. This is no longer the case. It turns out that in /clr...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
6
by: Ajay Martin | last post by:
Why would it be reasonable for someone to argue that it is incorrect to allow a public member inherited from a public base class to be redefined as private?
6
by: MattWilson.6185 | last post by:
Hi, I'm trying to find out if something is possible, I have a few diffrent lists that I add objects to and I would like to be able to have a wrapper class that won't affect the internal object, for...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
2
by: Fred | last post by:
I've got the following code: #include <iostream> class Base{ private: virtual void f(int) { std::cout << "f in Base" << std::endl; } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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...

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.