Identify only some inherited methods in a derived class

I came across an interview question related to OOPS. Here's the question: There is a base class Awith 5 methods. Now how should I create a class in such a way that if a class Binherits a class A, only 3 methods are displayed. And if the class Cinherits the class A, the other 2 methods are expanded.

Any thoughts ??

+4
source share
8 answers

This should not be possible in any object-oriented language, otherwise it would violate the principle of Liskov replacement . Substituting a Bfor Ashould not reduce its correctness (which means that methods should not be suddenly inaccessible)

, - , - "" . , :

  • ""?
  • 5 A?
  • "" C (, )

, internal, ..

+3

, . . .

, :

public class A {} //why we even need this class?

public class B : A, I3Methods
{
    public void Method1() { }
    public void Method2() { }   
    public void Method3() { }
}

public class C : A, I2Methods
{
    public void Method4() { }
    public void Method5() { }
}

public interface I3Methods
{
    void Method1();
    void Method2();
    void Method3();
}

public interface I2Methods
{
    void Method4();
    void Method5();
}
+1

, , , A, B C... , ... .

+1

A , 2 , :

namespace the_impossible
{
class Program
{
    static void Main(string[] args)
    {
        B b = new B();
        C c = new C();

        b.m1();
        b.m2();
        b.m3();

        c.m4();
        c.m5();
    }
}

namespace A_1
{
    public partial class A
    {
        public  void m1() { }
        public  void m2() { }
        public  void m3() { }
    }
}

namespace A_2
{
    public partial class A
    {
        public void m4() { }
        public void m5() { }
    }
}

class B : A_1.A 
{

}

class C : A_2.A
{ 

}
}

enter image description hereenter image description here

+1

, .

:

3 , B. 2 C.

0

, 5 . C# 5 , , , , .

class A
{
    protected void M1() { }
    protected void M2() { }
    protected void M3() { }
    protected void M4() { }
    protected void M5() { }
}

class B : A
{
    public new void M1()
    {
        base.M1();
    }

    public new void M2()
    {
        base.M2();
    }

    public new void M3()
    {
        base.M3();
    }

}

class C : A
{
    public new void M4()
    {
        base.M4();
    }
    public new void M5()
    {
        base.M5();
    }
}
0

, , . ++ . , A:

class A {
public:
    int Method1() {     return 1;   }
    int Method2() {     return 2;   }
    int Method3() {     return 3;   }
    int Method4() {     return 4;   }
    int Method5() {     return 5;   }
};

B, ( , B A, B).

class B: private A {
public:
    // We want to expose methods 1,2,3 as public so change their accessibility
    // with the using keyword
    using A::Method1;
    using A::Method2;
    using A::Method3;

};

C, :

class C: private A {
public:
    using A::Method4;
    using A::Method5;

};

, C, , :

class C: public A {
public:

};

:

B *b = new B();
b->Method1();  // This works, Method1 is public
b->Method4();  // This fails to compile, Method4 is inaccessible

, , , , B A:

A *brokena = b;  // This wouldn't compile because the typecast is inaccessible

A *a = (A*)b;    // This however does work because you're explicitly casting
a->Method4();    // And now you can call Method4 on b...
0

, . :

Define class A as a base class. They have intermediate child classes A1 โ†’ M1, M2, M3 and A2 โ†’ M4, M5 obtained from class A

Now you can 1) Class B inheriting A1 2) Class C inheriting A2

These two classes are still derived from class A.

And also we do not violate the principle of the Liskov signature.

Hope this gives clarity.

0
source

Source: https://habr.com/ru/post/1589237/


All Articles