I have a base class kaysonsSPdetails. which is internal and DemoDVD which is public and inherits SPdatabase.

internal class kaysonsSPdetails
{
    /// <summary>
    /// This method is used to connect to databse is returns an open connection
    /// </summary>
    private System.Data.DataSet connecttodabase()
    {
        throw new System.NotImplementedException();
    }
}


public class DemoDvd : global::kaysonsSPdetails
{
}

I am getting the following error

Inconsistent accessibility: base class 'kaysonsSPdetails' is less accessible than class 'DemoDvd' D:\arsheena\App_Code\DemoDvd.cs 7

I when i make kaysonsSPdetails public it is working fine but accessible to all. I donot want to make it accessible to all how to i do it.

If you inherit, the inherited should have the same visibility than the inheritant.

If really the only contents is the connecttodatabase, best use private to declare the kaysonsSPdetails class and then use public to declare the connecttodatabase.

To use this class, then you can use

public class DemoDvd 
{
var k = new global::kaysonsSPdetails();

private void OpenConnection()
{
k.connecttodatabase();
}

.
.

}

Hope this helps

It's a requirement of the language itself that base classes must be at least as accessable as classes derived from them. This means if you wish a public child class, the parent class must be public also.

You can write a class that contains your base class (has an instance of) rather that being derived from the base class. Then you can provide accessor methods to what you want exposed to the public.

You can declare the base class abstract, and then no one can create an object of that type.

Why don't you want someone using the base class?

@lolafuertes: in the class kaysonsSPdetails there are multiple methods and constructors
If really the only contents is the connecttodatabase, best use private to declare the kaysonsSPdetails class and then use public to declare the connecttodatabase. so in my case this scenario does not work.do you have any other alternative.


@Momerath: We want our parent class to be private and no instance should be created of our parent class but it cannot be abstract too since it contains multiple methods to interact to database

It can be declared abstract and still have no abstract methods. All that will do is prevent someone from using the class directly.

@Momerath: It is not an abstract class as it contains methods to connect to database. I cannot implement the same methods in other classes , hence cannot make it abstract and cannot create an instance of the class. hence, cannot make it public. its something like protected class in cpp(c++ language)

You can make it abstract, and you don't have to implement the methods in other classes. You only need to implement the methods if you wish to override the behavior in the base class.

Abstract classes can contain full methods. The only thing that will happen when you declare it abstract is that you won't be able to create an instance of the class. Your child classes will have full access to the methods of the abstract base class, you won't have to do anything.

Try this code and you'll see what I mean:

using System;

namespace TestBed {
    class Program {
        static void Main(string[] args) {
            ChildClass c = new ChildClass();
            Console.WriteLine(c.GetValue());
           
            Console.ReadLine();
        }

    }

    abstract class BaseAbstract {
        public int GetValue() { return 3; }
    }

    class ChildClass : BaseAbstract {
    }
}

Edited 5 Years Ago by Momerath: n/a

@Momerath thanks but I do not have to make it abstract. I want that I can access the class only with the class that inherent the class but noone can make objects of it. I have to implement methods in the class.

Edited 5 Years Ago by arsheena.alam: n/a

That is exactly what abstract classes do. No one can make instances of it, they can only make instances of classes derived from the abstract class.

This article has been dead for over six months. Start a new discussion instead.