How do I know when to inherit a class vs instantiating an object of a class?

Hello, beginner here. If I need something from a different class, how will I know if I should inherit from it or I make a new object of it? I’m a little confused about that.

I’m looking at this Stack Overflow question: c# - What is difference between initiate a class and inherit a class - Stack Overflow

I’m still a bit confused. What questions should I be asking myself in knowing when to use what?

I mean if you need a method from a particular class while working in a different class, either inheriting or instantiating will allow you to use it, right?

Well they are two different things used in two very different ways.

You inherit from a class, usually, to gain/override the functionality provided from the more base class. In Unity, for example, the default script inherits from Monobehaviour because its assumed that most of the time you’ll be making a component attached to a game object, that requires the usual Unity messages such as Awake, Start, Update, etc. But you can then inherit from other classes, say, ScriptableObject, when you need to make a class with different properties. You can also inherit from no class and have a vanilla C# class.

It’s worth noting, in Unity you can’t instantiate Monobehaviours or ScriptableObejcts or most if any of the stuff that has something going on in the managed C++ land with the usual syntax of ‘= new YouClassName’. Instead you use AddComponent or CreateInstance in respective order.

Instances are used for, well, when you need a copy to work with. And this can work in tandem with inheritance, as they’re rather different tools that you use in code.

So for example you can outline a base class with a virtual method like so:

public class SomeClass
{
    public virtual void DoStuff()
    {
        //Do Stuff
    }
}

Then you can inherit from the class to override the virtual method:

public class SomeDervidedClass : SomeClass 
{
    public override void DoStuff()
    {
        //Do Different Stuff
    }
}

Then in another script, you can create instances of these and use them:

public class SomeMonobehaviour : Monobehaviour
{
    private SomeDerivedClass derivedClass;

    private void Start()
    {
        derivedClass = new SomeDerivedClass(); //here you're instantiating a new copy of the class, assigned to a private field
        derivedClass.DoStuff(); //and now you're doing stuff with the instantiated copy
    }
}

The best way I can put it with my limited skill, is that you inheriting is part of the nature of a script. And then you instantiate classes when you need to put them to use. It’s not really a case of one or the other.

When you want something to be able to do something that exists in another class, then you may inherit from that class, like we do with Monobehaviour. When you want to do something with that class, then you work with instances of it.

Hopefully that makes sense. Maybe someone here with more experience can outline this better.

So I guess the inheritance part makes sense, the instantiating part might be a bit fuzzy for me… so far I’m coming up with:

“Do I need extra functionality from other existing classes?” = Inheritance

“Do I need to manipulate this class?” = Instantiation

I think I might need a better question to ask myself in regards to knowing when to instantiate. I mean intuitively I kinda know: you have an orc class inherit from an enemy class. But you instantiate an orc class as Bob but not a formal understanding as to why and even less so with less familiar contexts.

One website mentions:

7601011--943327--upload_2021-10-25_9-3-53.png

7601011--943333--upload_2021-10-25_9-4-37.png

4 is helpful but for 7, what does: “Class does not contain any values which can be associated with the field” mean? I mean constructors and properties are values associated with a field, right?

There is almost NEVER a reason to do this in the first 20 or 30 games. Put simply it is an advanced software engineering technique that can give you great advantage in coding, but without the capacity to reason it is likely to tie your shoelaces and cause you great distress and derail your development.

Just forget about inheritance until you’ve published a few games.

Instantiation in the context of Unity means “make this thing real in my scene.” Unity Instantiates your scene for you when you load it. You can write code to Instantiate more things. Or not. It could be entirely static gameplay within the scene with what exists (think, pong game: ball, paddle, net, boundary, that’s it.).

When you write a class, you’re writing what it does. When you work with an instance (eg ‘= new YourClassName()’), you’re working with what it’s doing.

For example, when you’re writing a monobehaviour component, you’re writing what that class does. When you attach it to a game object, you’re working with what it’s doing.

It isn’t such much of case of where do you use one over the other. You use both, at the same time, for different reasons.

I notice you quoting those somewhat dry Youtube Unity videos introducing certain C# concepts through the lens of video games/Unity. The best advice I can give you is to not do inheritance like they show in that particular video. Unity is a component based engine and there’s no reason to do inheritance like they suggest.

Most of the time you’ll be doing shallow (and by shallow, I mean one inheritance step deep) but wide hierarchies, usually to give the child classes access to a ‘sandbox’ of functionality provided by the parent class.

Honestly I feel like you’re kind of over thinking this.

Can I kindly disagree with you here? I wouldn’t call inheritance that advanced a topic and I certainly haven’t had to publish a silly amount of games to put it to good use. ;p

Everybody is always welcome to disagree with me, especially you Spiney!

I asserted the above in the context of the level of software development proficiency implied by the subject line of this post.

In my experience teaching, inheritance is far too abstract and vague at this stage. Save it for when its power can multiply the power of the engineer, not confuse and confound it.

YMMV!

My $.02: If you’re a newbie and you aren’t a CS major, inheritance is an advanced topic for you. If you aren’t a newbie or you are a CS major, inheritance isn’t an advanced topic for you. And there are some people, who has natural inclination towards programming and understand programming concepts very easily, they probably should count as a CS major in this context.