Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public class Hide
{
    public void method1()
    {
     Console.WriteLine("method 1 called");
    
    }
    public void method2()
    {
     Console.WriteLine("method 2 called");
    }
}
public class Base : Hide
{
//here i have to only acess method1()
}
public class Child : Hide
{
//here i have to only acess method2()
}


Is it possible or not??
Posted
Updated 26-Sep-12 3:41am
v3
Comments
I.explore.code 26-Sep-12 9:59am    
I don't think so. All public methods are by default always available to the deriving classes. You can try creating both methods as virtual and then override the ones that you need in each child class. Just make sure not to call the non-overridden methods because they would still be available.
chinta123 26-Sep-12 10:05am    
Can you please tell me whether I can access private class member function into another class or child class?
[no name] 26-Sep-12 14:04pm    
The private members are known only in the current type.

1 solution

Not the way you have written it. What you want to do is a little backwards in your thinking.
Public methods in your base class will be available to all classes which inherit from it.
If you need a method to be available only to a specific child, then you must define that method only in that class and not the base.
Note that your naming convention is a little strange. See my example for what I think you are aiming at.
For example:
C#
public class Base
{
   public void Method1(){ Console.WriteLine("Method 1 called."); }
   abstract void Method2();
}
public class Hide : Base
{
   // Method1 is available because of inheritance
   // Method2 must be overriden in this class because it has been defined in base as abstract
   public override void Method2(){ Console.WriteLine("Method 2 called in Hide."); }
   // Method3 is only available in Hide
   public void Method3(){ Console.WriteLine("Method 3 called."); }
}
public class Child : Base
{
   // Method1 is available because of inheritance
   // Method2 must be overriden in this class because it has been defined in base as abstract
   public override void Method2(){ Console.WriteLine("Method 2 called in Child."); }
   // Method4 is only available in Child
   public void Method4(){ Console.WriteLine("Method 3 called."); }
}
 
Share this answer
 
Comments
chinta123 26-Sep-12 10:03am    
can we access Private method () into another class??please tell me ..
fjdiewornncalwe 26-Sep-12 10:05am    
No, that is why they are private. You should read up on access modifiers to understand this. A private method is only available to the class it is in and not tho children or parents. That being said, one can technically use the System.Reflection namespace to "break" these rules and gain access to private members of a class. That namesapace is NOT intended for use in "normal" coding.
chinta123 26-Sep-12 10:10am    
thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900