[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

Class accessibility question

if a have a class defined as:

public class ClassB : ClassA
{

}

any objects of ClassB i create will have access to the members of ClassA. how do i prevent that? i want the members of ClassA to be accessible from within the methods of ClassB, but not any objects of ClassB.
0
RepriseMIS
Asked:
RepriseMIS
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Don't Inherit from ClassA then...instead declare an Instance of ClassA inside ClassB as a private member.  Then make your public Functions of ClassB access the private instance of ClassA in the manner you desire...
0
 
PockyMasterCommented:
Make the members of classA protected, in that way they can only be accessed by derived classes, but derived classes will not expose them
0
 
RepriseMISAuthor Commented:
PockyMaster, can you give me an example of how this could be implemented? thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PockyMasterCommented:
using System;

namespace ConsoleApplication1
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                        
                  ClassB b = new ClassB();
                  Console.WriteLine( b.GetValue());

                  b.Increase();
                  Console.WriteLine( b.GetValue());

            }

      }


      class ClassA
      {
            protected  int m_iSomeValue;

            public void SomeMethodYouLikeToExpose()
            {
                  //implement this
            }
      }

      class ClassB : ClassA
      {
            public void Increase()
            {
                  //increase the protected member in your base class
                  m_iSomeValue++;
            }

            public String  GetValue()
            {
                  //you are still allowed to return it
                  //but you cannot access it directly
                  //in instances from class B
                  return m_iSomeValue.ToString();
            }

      }
}


0
 
RepriseMISAuthor Commented:
thanks. one more question: the protected members of ClassA can still be accessed by an object of class A correct?

ClassA clsA = new ClassA();
clsA.m_iSomeValue++;
0
 
PockyMasterCommented:
Yeah, I don't know if you want to have that or not, if you want to prevent that change
class ClassA into:

abstract class ClassA

in that way people are not able to create instances directly from classA
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now