14

I've got the code below and I'm trying to do some inheritance exercises but when I try to run this code it gives me an error:

Inconsistent Accessability: Base Class is less accessible than class

The code:

class Program
{
    static void Main()
    {

        FoodProducts Test = new FoodProducts();

        Test.Limit();



    }
}

public class FoodProducts : Products
{
    public void FoodProduct()
    {
        Console.WriteLine("This is food product");
    }

    public void Limit()
    {
        Console.WriteLine("This is an Attribute of a Product");
    }

}

Would someone be able to help me?

Alex M
  • 2,756
  • 7
  • 29
  • 35
user1618490
  • 771
  • 3
  • 8
  • 13
  • 1
    Please add the products class definition, specifically the declaration line. I'm guessing it's declared private. – Khepri Aug 27 '12 at 02:52

11 Answers11

24

Just for future reference for someone thick like me, I got this in the following situation and couldn't figure out what was going wrong:

public class Foo : Base<Bar> {} <-- Inconsistent accessibility

public class Base<T> {}

It took me a while to work out that the culprit was here:

internal class Bar {}
Benjol
  • 63,995
  • 54
  • 186
  • 268
21

What line is the error on, and what is the specific error text? Also, where is the definition of Products?

You are probably getting CS0060: "Inconsistent accessibility: base class 'class1' is less accessible than class 'class2'" Thus, I'm assuming your Products class is not marked as public.

This problem happens when a base class is marked as something other than public (internal, for example), but then you try to make a public derived class.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • 11
    Be great to explain why this is not allowed, i.e. what is wrong with a public class inheriting from a hidden base class. – dumbledad Jan 25 '17 at 09:49
  • 1
    @dumbledad I wrote an answer about the why in [here](https://stackoverflow.com/a/50477805/728675). – RayLuo May 22 '18 at 23:28
15

Lots of answers here suggest to change your base class into public too. In a sense they are correct, but then they miss an equally valid alternative, which is to change your derived class to internal (to match what the base class's accessibility).

So, the real (yet short) answer is to let your base class and derived class to have same accessibility.

For those who wonder why, the long answer is: when a public derived class attempts to inherit an internal or private base class, it would argurably (more on this later) become semantically unclear whether the sub-class would also want to expose those public methods in the internal/private base class. Eric Lippert gave an detailed explanation in his blog post: Why is deriving a public class from an internal class illegal?, quoted below (with minor edit):

On the one hand, it is a public method of a base class, and so it seems like it should be accessible (to the derived class too). On the other hand, the fact that Base is internal is evidence that its internal method is supposed to be inaccessible outside the assembly. A basic design principle of C# is that when the intention is unclear, the compiler brings this fact to your attention by failing.

PS: That being said, one may argue that, the compiler could possibly just stick with "one of those 2 hands" and it would still be deterministic. So why was the design decision NOT chosen on one specific way? For such a follow-up question, the answer is, it all boils down to design philosophy that (again, quoted from Eric's blog post):

this rule of the language encourages you to use inheritance relationships to model the business domain semantics rather than as a mechanism for code reuse.

So that was the choice C# already made.

Lastly, for the sake of completeness, it is worth to mention that, the design philosophy above is not necessarily the universal and only way to use inheritance. (Heck, some other languages do not even have the public/internal concept in the first place and they are still successful). Personally I see nothing wrong if we would also want to use inheritance mechanism for code reuse. But C# already chose to only use inheritance for business domain semantics. So, it is what it is.

RayLuo
  • 17,257
  • 6
  • 88
  • 73
  • 2
    I am not following this logic... I don't see why intention is unclear, and it could be nothing related to inheritance-as-code-reuse. It is simple: public-internal method should be public internally and inaccessible externally, as it happens in all other cases. The _On the one hand, it is a public method of a base class, and so it seems like it should be accessible_ - makes no sense when the base is internal. – kan Apr 02 '19 at 16:29
  • 1
    @Kan, so your thought is more about Eric's blog post (quoted in my answer above), rather than about my answer here. Personally I don't necessarily agree with that blog post either (as you can tell from the tone I wrote this answer). In that sense I am NOT advocating on either side. I was merely representing relevant information for future readers - like you - to think and learn and draw your own conclusion. It seems that I reached my goal here. :-) – RayLuo Apr 03 '19 at 22:54
  • I think the same as @kan here ... and hope this restriction will be removed in the future – kofifus Sep 24 '19 at 23:49
6

Probably the class Products is not public. Add public to the Products class definition.

If you have something like:

class Products {
 ...
}

The C# compiler interprets the Products class as internal.

Candide
  • 30,469
  • 8
  • 53
  • 60
  • The default visibility for classes is [`internal`](http://stackoverflow.com/questions/3763612/default-visibility-for-c-sharp-classes-and-members-fields-methods-etc). – Jonathon Reinhart Aug 27 '12 at 03:14
3

Add the public directive to the class you are trying to inherit from.

Gregg Bursey
  • 1,097
  • 2
  • 11
  • 16
AndyC
  • 1,325
  • 1
  • 13
  • 23
0
public class Products

Make you class public(as shown above) in order for it to be inherited or accessible.

Riash.R
  • 1
  • 3
0

You can also add this before the definition of the namespace in your base class (right after the last "using ..." line):

[assembly: InternalsVisibleTo("<name of the assembly of the caller class>")]
0

You have to maintain the same access modifier. E.g., if you have a public access modifier on the parent, then assign the public modifier to the child class.

Parent class:

public class Test {
    public void calculations() {
        console.WriteLine("Done");
    }
}

Child Class:

public class Child : Test {
    static void Main() {
        Child objChild= new Child();
        objChild.calculations();
    }
}

Or else observe the access modifier deference below:

Parent class:

class Test{
    public void calculations() {
        console.WriteLine("Done");
    }
}

Child Class:

class Child : Test {
    static void Main() {
        Child objChild= new Child();
        objChild.calculations();
    }
}
Patrick Mcvay
  • 2,221
  • 1
  • 11
  • 22
Anitha
  • 1
-1

That happens when, for example, the base class is private, but the derived class is public. A contradiction, so to speak.

Midrel
  • 44
  • 2
-1

this means if you wish a public child class, the parent class must be public also.

programmer
  • 83
  • 11
-1

One of the probable reason for this issue may be , you have more than one main class , Make sure you have only one main class.