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 ...
inheritance c # interface
RameshVel
source share