Protected method access from derived class

user3165438

I have a protected method in base class :

public class BaseClass
{
  protected virtual void Foo(){}
}    

The method is overrided one of the derived classes:

public class Derived1 : BaseClass
{
   protected override void Foo()
    {
     //some code...
    }
}    

Another derived class has an instance of the first derived class.
When I try to access the Foo method (existing in base class, as mentiond) I get an error :

 public class DerivedClass2 : BaseClass
    {
     BaseClass instance = new DerivedClass1();
     instance.Foo(); // Here I get an error
    }

The error I get:

Error CS1540: Cannot access protected member 'BaseClass.Foo' via a qualifier of type 'BaseClass';   
the qualifier must be of type 'DerivedClass2' (or derived from it)

I understand that protected members should not give up their value to any other instance, even an instance derived from the same type,
but is there a way not to modify the method as public?

Suresh Kumar Veluswamy

You can make the Foo method declaration as protected internal....

public class BaseClass
{
  protected internal virtual void Foo(){}
} 

public class Derived1 : BaseClass
{
   protected internal override void Foo()
    {
     //some code...
    }
}

Here "protected internal" means the member is visible to any class inheriting the base class, whether it's in the same assembly or not. The member is also visible via an object declared of that type anywhere in the same assembly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot access protected members from a derived class

From Dev

Derived class can't access protected method of base class

From Dev

C++: cannot access protected member from derived class

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

From Dev

Why a derived class can't access a protected getter from the base class?

From Dev

How to access Interface method implemented in the derived class from child class?

From Dev

Access to base class method only from the derived class

From Dev

Can't access protected method from child class

From Dev

Confusion regarding protected member functions and derived class access

From Dev

Why i can't access protected property in the derived class

From Dev

Can't call virtual protected method in derived class

From Dev

Accessing protected member in base class from a derived class

From Dev

Accessing protected member in base class from a derived class

From Dev

Unable to access protected final synchronized method in a class

From Dev

Why can't we call protected destructors from a derived class?

From Dev

Polymorhism - how to access method of derived class?

From Dev

Access to atributes from derived class in the base class

From Dev

Access base class object from derived class

From Dev

C++ public method inherited from base class can not access private member variable in derived class

From Dev

Can't access derived class method from pointer of type base class

From Dev

Is it possible to access a derived class's method from a base class by using $cast in systemverilog?

From Dev

Clone derived class from base class method

From Dev

Base class calls method from derived class?

From Dev

Clone derived class from base class method

From Dev

access protected member from separate class

From Dev

access protected member from separate class

From Dev

Connect to protected slot in derived class

From Dev

Protected function not accessible in derived class

Related Related

  1. 1

    Cannot access protected members from a derived class

  2. 2

    Derived class can't access protected method of base class

  3. 3

    C++: cannot access protected member from derived class

  4. 4

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  5. 5

    Can a derived class access a private method of a protected inner class of the parent class that is a friend of the inner class?

  6. 6

    Why a derived class can't access a protected getter from the base class?

  7. 7

    How to access Interface method implemented in the derived class from child class?

  8. 8

    Access to base class method only from the derived class

  9. 9

    Can't access protected method from child class

  10. 10

    Confusion regarding protected member functions and derived class access

  11. 11

    Why i can't access protected property in the derived class

  12. 12

    Can't call virtual protected method in derived class

  13. 13

    Accessing protected member in base class from a derived class

  14. 14

    Accessing protected member in base class from a derived class

  15. 15

    Unable to access protected final synchronized method in a class

  16. 16

    Why can't we call protected destructors from a derived class?

  17. 17

    Polymorhism - how to access method of derived class?

  18. 18

    Access to atributes from derived class in the base class

  19. 19

    Access base class object from derived class

  20. 20

    C++ public method inherited from base class can not access private member variable in derived class

  21. 21

    Can't access derived class method from pointer of type base class

  22. 22

    Is it possible to access a derived class's method from a base class by using $cast in systemverilog?

  23. 23

    Clone derived class from base class method

  24. 24

    Base class calls method from derived class?

  25. 25

    Clone derived class from base class method

  26. 26

    access protected member from separate class

  27. 27

    access protected member from separate class

  28. 28

    Connect to protected slot in derived class

  29. 29

    Protected function not accessible in derived class

HotTag

Archive