Preventing method access in derived class.
-
Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.
-
Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.
private methods cannot be overridden.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.
-
Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.
Diana Fernandez wrote:
Is it possible to prevent accessing a method in derived class?
Yes. Go with Private Access Modifier.
Regards, Satips.:rose:
-
Hi Is it possible to prevent accessing a method in derived class? Since "sealed" keyword is only used along with "override" I can not use this option in the base class. Thanks, Diana.
Consider the scenario given below public class Base { protected string _name = null; public void SetName(string Name) { _name = Name; } } public class Derived : Base { public void SetName(string Name, bool Condition) { if (Condition) _name = Name + "true"; else _name = Name + "false"; } } How is it possible to prevent accessing the Base class method on the object of derived class? Because base class implementation should not be used on the object of derived class.
-
Consider the scenario given below public class Base { protected string _name = null; public void SetName(string Name) { _name = Name; } } public class Derived : Base { public void SetName(string Name, bool Condition) { if (Condition) _name = Name + "true"; else _name = Name + "false"; } } How is it possible to prevent accessing the Base class method on the object of derived class? Because base class implementation should not be used on the object of derived class.
Hello, in this case you should declare it as virtual und override method.
public class Base
{
protected string _name = null;protected virtual void SetName(string Name) { \_name = Name; }
}
public class Derived : Base
{
protected override void SetName(string Name, bool Condition)
{
if (Condition)
_name = Name + "true";
else
_name = Name + "false";
}
}All the best Martin
-
Hello, in this case you should declare it as virtual und override method.
public class Base
{
protected string _name = null;protected virtual void SetName(string Name) { \_name = Name; }
}
public class Derived : Base
{
protected override void SetName(string Name, bool Condition)
{
if (Condition)
_name = Name + "true";
else
_name = Name + "false";
}
}All the best Martin
That will not compile, the method signatures differ.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Consider the scenario given below public class Base { protected string _name = null; public void SetName(string Name) { _name = Name; } } public class Derived : Base { public void SetName(string Name, bool Condition) { if (Condition) _name = Name + "true"; else _name = Name + "false"; } } How is it possible to prevent accessing the Base class method on the object of derived class? Because base class implementation should not be used on the object of derived class.
Diana Fernandez wrote:
Because base class implementation should not be used on the object of derived class.
It's a feature of OO ( and the whole point of inheritance ) that derived classes DO inherit the base methods. The best you could do is make SetName virtual on the base class, and override it on the derived, so it calls the new method with a default condition, or have it throw an error. public override SetName(string Name) { SetName(name, true); }
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
That will not compile, the method signatures differ.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Diana Fernandez wrote:
Because base class implementation should not be used on the object of derived class.
It's a feature of OO ( and the whole point of inheritance ) that derived classes DO inherit the base methods. The best you could do is make SetName virtual on the base class, and override it on the derived, so it calls the new method with a default condition, or have it throw an error. public override SetName(string Name) { SetName(name, true); }
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Or use aggregation instead of inheritance - maybe in combination with an interface.
-^-^-^-^-^- no risk no funk