What is the Utility of Parent Class Method Hiding in Inheritance?

Vijay Nayar madric at gmail.com
Mon Jan 14 09:10:39 UTC 2019


https://dlang.org/spec/function.html#function-inheritance

Consider this snippet from the documentation:

class A
{
     int foo(int x) { ... }
     int foo(long y) { ... }
}

class B : A
{
     override int foo(long x) { ... }
}

void test()
{
     B b = new B();
     b.foo(1);  // calls B.foo(long), since A.foo(int) not 
considered
     A a = b;

     a.foo(1);  // issues runtime error (instead of calling 
A.foo(int))
}


I ran into this the other day, where I had a function of the same 
name in a child class, and found that all functions in the parent 
of the same name now became hidden, unless I add an alias 
statement.

After a bit of reading, I understood the rule and how it works, 
but what I'm missing is the "why".  Why is it desirable to hide 
methods from a parent class which have the same name (but 
different arguments) as a method in a class?


More information about the Digitalmars-d-learn mailing list