views:

78

answers:

4

Can we hide the derived class public method so that it is not accessible in Main() function in C#. We can't change the accessors of the derived class method.

public class A
{
    public void Add()
    {
    }    
}       

public class B : A 
{
    public void Multiply()
    {
    }        
}

In main() method in C#

B b = new B();
b.mulitply(); // should give compile time error... Like method not founded.

Is there any way we can do it.

+1  A: 

This request doesn't make any sense. Class B declares a method Multiply. Why would this method ever be hidden? Hidden from whom?

John Saunders
A trick to implement this is A a = new B();a.multiply() give error()
A.P.S
Hidden from misuse -- for example for a lot of classes ToString does not make sense (and what worse, some other classes can invisible make use of this method), yet I cannot hide it and each time I have to track stupid bugs. It is real pain.
macias
@A.P.S: That works fine until someone changes "A" to "B".
John Saunders
@macias: What about polymorphism? ToString(), by definition, _always_ "makes sense". Whether the output is useful is a separate question.
John Saunders
@John, nope -- it always **works**, however it seldom makes sense. And it is too easy to make a mistake without even knowing it. Recently I decided to throw exception for every method from object, because it is too dangerous to let it go unnoticed -- I prefer being bitten right at the spot.
macias
@macias: too dangerous? That makaes no sense. You override Equals to throw an exception?
John Saunders
@John, not till now, but I will (as default). Leaving it not implemented leaves hole in whole application, somebody finally will assume it is implemented (and thanks to object compiler won't protest) and will get strange results (at best). IOW -- I will implement it ONLY when it makes sense and there is solid logic behind it (it applies to all methods from object class).
macias
@macias: you don't understand. It is already implemented in `object`. The implementation _always_ works, and it _always_ makes resonable sense. That's what polymorphism is about. Can you give an example where you feel that strange results would occur?
John Saunders
@John, no, you don't understand :-DDD It seldom makes sense -- when "make sense" means semantic sense, not syntactic. string s = "hello"+world. Does it work? Yes. Does it make sense? Not at all. It is simply typo, it should be string s = "hello"+"world". (1) it is mistake to assume everything is convertible to string (2) it works as implicit casting/conversion, and implicit castings are bad!
macias
@macias: hint: you're the only one I've ever heard of who complains about this. Lot of .NET devs out there, only one complains: "a word, to the wise, is sufficient" (anon)
John Saunders
@John, there are a lot of texts written about implicit casting. In C++ world it was curse, I doubt it is all of the sudden virtue in C#/.Net. However, since we reach the point of playing majority/minority argument, I think it is good thing to end here.
macias
@macias: not majority vs. minority, just everyone else vs. you
John Saunders
+5  A: 

Absolutely not. It's a public method of a public class. If you shouldn't be able to use it, it shouldn't be public.

(I don't see how the fact that it derives from A is relevant, by the way.)

Jon Skeet
Well, "A" class may be not your product (i.e. you don't have control over it).
macias
@macias: Sure, you cant change `System.Object` either... there's nothing *really* related to inheritance in the question, as far as I can see.
Jon Skeet
Not in the example, but in the questions? "Hiding derive class method?" and "Can we hide the derived class public method so that it is not accessible in Main() function in C#". Inheritance is all about derived classes.
macias
@macias: The question only talks about "derived class public method" - not "a public virtual method in the base class, overridden in the derived class". Given the lack of anything explicit *and* the different methods in the sample code, I think it's a stretch to *assume* the OP is really talking about an overridden method.
Jon Skeet
@Jon, by all means, I don't want to be (too) picky, but since every class derives from the object it is valid to consider problem with hiding methods from object class as well. And the code is just **an example** of the problem.
macias
A: 

Why don't you just just make the method private?

public class B: A
{
  private void Multiply()
  {
  }
}

If you absolutely can't change B, then the only thing I can think of is creating a full adapter class.

public C
{
  private B instance;

  private void Multiply(){}

  public void Add()
  {
    B.Add();
  }
}

That will force you to "replicate" all the methods in B excepts the one you want.

If you ABSOLUTELY need the class to be "B" you'll have to reverse engine it with reflector and change public field to private or protected.

Jorge Córdoba
We can't change the public accessors, some one asked this question in the interview? :-D. I said the same,but he didn't agree.
A.P.S
Because it won't work. If you override it cannot be private, if you define this as new it will be ignored (i.e. inherited version will be called).
macias
@macias: There's no overriding going on. Look at the method names: Add and Multiply. Heck, neither of them are even specified as being virtual.
Jon Skeet
The only thing I can't think about is a full Adapter on top of B and then using only C as the way to interface with B.
Jorge Córdoba
@Jon, I missed missing virtual here, and... gee.. 3 lines distance and all I saw, was "math" :-D
macias
A: 

The point of function being public is that it can be accessed elsewhere.

You could either inherit privately ? ( doesnt make sense) or declared the member as private or protected so derived class doesnt inherit it.

Sii
You can't do this for object -- you always inherit from object.
macias
I think you are understanding it wrong. Once you inherit the class as private, the data members and methods become inaccessible and a compile time error will be displayed.Objects are just instances of classes you dont inherit objects.
Sii
Misunderstanding -- the class "object", not the object of the class. The "object" class is the basic class in C#, and all other types inherit from it.
macias
@MrProg: C# doesn't have private inheritance.
John Saunders