Is there a way to hide methods partially in child classes?

This question seems strange, but I came across this question in an interview recently.

I was asked if there is a way in C # to hide methods partially in inherited child classes ?. Suppose that base class A is represented in 4 ways. Class B implements A, and it will have access only to the first two methods, and to implement the class CA will only have access to the last 2 methods.

I know we can do it

public interface IFirstOne { void method1(); void method2(); } public interface ISecondOne { void method3(); void method4(); } class baseClass : IFirstOne, ISecondOne { #region IFirstOne Members public void method1() { throw new NotImplementedException(); } public void method2() { throw new NotImplementedException(); } #endregion #region ISecondOne Members public void method3() { throw new NotImplementedException(); } public void method4() { throw new NotImplementedException(); } #endregion } class firstChild<T> where T : IFirstOne, new() { public void DoTest() { T objt = new T(); objt.method1(); objt.method2(); } } class secondChild<T> where T : ISecondOne, new() { public void DoTest() { T objt = new T(); objt.method3(); objt.method4(); } } 

But what they wanted is different. They wanted to hide these classes from inheritance from base points. something like that

 class baseClass : IFirstOne, ISecondOne { #region IFirstOne Members baseClass() { } public void method1() { throw new NotImplementedException(); } public void method2() { throw new NotImplementedException(); } #endregion #region ISecondOne Members public void method3() { throw new NotImplementedException(); } public void method4() { throw new NotImplementedException(); } #endregion } class firstChild : baseClass.IFirstOne //I know this syntax is weird, but something similar in the functionality { public void DoTest() { method1(); method2(); } } class secondChild : baseClass.ISecondOne { public void DoTest() { method3(); method4(); } } 

Is there a way in C #, we can achieve something like this ...

+6
inheritance c # interface
source share
6 answers

I did this with 1 main base class and 2 auxiliary bases.

 // Start with Base class of all methods public class MyBase { protected void Method1() { } protected void Method2() { } protected void Method3() { } protected void Method4() { } } // Create a A base class only exposing the methods that are allowed to the A class public class MyBaseA : MyBase { public new void Method1() { base.Method1(); } public new void Method2() { base.Method2(); } } // Create a A base class only exposing the methods that are allowed to the B class public class MyBaseB : MyBase { public new void Method3() { base.Method3(); } public new void Method4() { base.Method4(); } } // Create classes A and B public class A : MyBaseA {} public class B : MyBaseB {} public class MyClass { void Test() { A a = new A(); // No access to Method 3 or 4 a.Method1(); a.Method2(); B b = new B(); // No Access to 1 or 2 b.Method3(); b.Method4(); } } 
+3
source share

Although you cannot do exactly what you want, you can use an explicit implementation of the interface to help, in which the members of the interface are displayed only if they are explicitly passed to that interface ...

+2
source share

Perhaps the interviewer may have referred to the concealment method ?

Here you declare a method with the same signature as in your base class, but you do not use the override keyword (either because you did not do this, or you cannot - as when using the method in the base class it is not virtual).

Hiding a method, as opposed to overriding it , allows you to define a completely different method - one that is accessible only through a reference to a derived class. If called through a reference to the base class, you call the original method in the base class.

0
source share

Do not use inheritance. This makes the public or protected objects of the base class available directly in the derived class, so it just doesn't want you to.

Instead, make the derived class an implementation of the corresponding interface and (if necessary) move the methods to a private instance of the base class. That is, use composition (or β€œaggregation”) instead of inheritance to extend the original class.

 class firstChild : IFirstOne { private baseClass _owned = new baseClass(); public void method1() { _owned.method1(); } // etc. } 

By the way, class names must begin with an uppercase letter.

0
source share

There are two solutions for hiding methods inherited from the base class:

  • As mentioned in thecoop, you can explicitly implement an interface that declares the methods you want to hide.
  • Or you can simply create these methods in a base class (not inherited from any interface) and mark them as private.

Sincerely.

0
source share

How about entering a base class like IFirst ?

 interface IFirst { void method1(); void method2(); } interface ISecond { void method3(); void method4(); } abstract class Base : IFirst, ISecond { public abstract void method1(); public abstract void method2(); public abstract void method3(); public abstract void method4(); } class FirstChild : IFirst { private readonly IFirst _first; public FirstChild(IFirst first) { _first = first; } public void method1() { _first.method1(); } public void method2() { _first.method2(); } } 

Injection does not allow you to violate the principle of separation of circuits . Pure inheritance means that your FirstChild depends on an interface that it does not use. If you want to keep only IFirst functionality in Base , but ignore the rest, then you cannot inherit only Base .

0
source share

All Articles