Shwarz
Shwarz

Reputation: 105

C# subclass or interface?

Pretty new to C# -- I was instructed to create a subclass to do a variation of the base class. Never having done sub classes before, I read a bit and then tried it out. I could only access the private property of MyClass from the sub class after inheriting from the base class. What have I done here? Is this a true sub class, or some sort of nested inheriting one?

    public class MyClass
{
     private string connString;
     // exec stored procedure 1

     public class MySubClass : MyClass
     {
            otherClass o = new otherClass(connString);
            // exec stored procedure 2
     }
}

Whatever I did, it seems to work. Also, while on the topic of sub classes, is there a common way to name them? e.g. MyClass_SubClass, or _MyClass etc?

Thanks much!

Edit: Thanks again, everyone! I think I was looking for this answer here. Regardless, I realized that I'd misunderstood the task -- no need for nesting or inheritance at all! Just created a separate similar class.

Upvotes: 1

Views: 1905

Answers (3)

Mick
Mick

Reputation: 6864

Your code is valid, it's not exactly implementing what you were instructed to implement. You can declare a class within a class. With your code you can instantiate both classes as follows.

class Program
{
    static void Main(string[] args)
    {
        MyClass myClass = new MyClass();
        MyClass.MySubClass mySubClass = new MyClass.MySubClass();
    }
}

This is pretty unusual, it's not uncommon to have a class declared within a class but normally you would not declare MySubClass as public, you would declare it as private or protected. Like so....

public class MyClass
{
    private string connString;
    private MyClassHelper mySubClass = new MyClassHelper();

    // exec stored procedure 1

    protected class MyClassHelper
    {
        object o = new object();
        // exec stored procedure 2
    }
}

...So MyClassHelper exists only within the scope of MyClass. I've never seen code in which an inner class inherits the outer class, it compiles perfectly, you would just never do it, you could easily create circular references.

BradleyDotNET's answer tells you how to implement a subclass of MyClass. This is also called inheritance read more about it here... http://msdn.microsoft.com/en-us/library/ms173149(v=vs.80).aspx

As a word of warning I wouldn't go too crazy with class inheritance, there are many types of relationships between classes, in comparison class inheritance is quite restrictive.

The title of your question is subclass or interface. Interfaces are something you should be looking at and understanding, they are very important especially for implementing SOLID code. In my code classes inherit interfaces far more commonly than they inherit classes.

Upvotes: 2

KC-NH
KC-NH

Reputation: 748

MySubClass is really MyClass.MySubClass. Normally, a plain subclass cannot access private members.

A normal subclass would be in a separate file as a standalone class. If the subclass is supposed to be permitted access to the base class members, then you would declare them as "protected" rather than "private".

One the subject of naming, usually the name represents the type, regardless of base class. For example, if you have a base class of Animal, it's reasonable that you have subclasses called Dog and Cat. Or in Windows, you could have a base class called Control and subclasses called Button, TextBox, Label and so on.

Upvotes: 2

BradleyDotNET
BradleyDotNET

Reputation: 61349

You have nested your classes, which is something you should probably avoid until you understand C# and OOP better (and then you inherit, which is just weird).

Subclassing or derivation, is accomplished through inheritance. In your example:

public class MyClass
{
     private string connString;
     // exec stored procedure 1
}

public class MySubClass : MyClass
{
      otherClass o = new otherClass(connString);
      // exec stored procedure 2
}

To allow MySubClass to access a member of MyClass, that member needs to be marked as protected or higher. private members cannot be accessed outside of the containing class.

public class MyClass
{
     protected string connString;
     // exec stored procedure 1
}

Upvotes: 6

Related Questions