Doubt in a C# question.

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
techgeekk
Posts: 1
Joined: 2021-03-24 10:18

Doubt in a C# question.

#1 Post by techgeekk »

Hey peeps,

I am a student and an aspiring software developer. Recently had built interest in practicing problems in C#. Got stuck up here at the 11th question in this article.
Would anyone be able to help me out with the multiple inheritance example they've explained here? Any help will be appreciated:)
https://www.interviewbit.com/c-sharp-in ... interfaces

User avatar
supersoup
Posts: 3
Joined: 2022-01-07 03:50

Re: Doubt in a C# question.

#2 Post by supersoup »

I'm not sure exactly what you're asking. You want a breakdown of how inheritance works in C#? A more specific question might be helpful.

Consider the following:

Code: Select all

// Let's pretend we're working on a game

// This is the base Entity class for things in the 
// game world, with a few fundamental variables.
class Entity // This will be our "superclass"
{
    // Private vars
    // Classes that inherit can't access these variables
    private string name;
    private Vector3 position;

    // Protected vars
    // Classes that inherit can access these variables
    protected Model3D model;
    protected Animator anim;

    // Public vars
    // Anybody, anywhere can access these variables
    public bool isActive;
    public string description;

    
    // The rule of thumb is that you want to only expose 
    // methods and variables that you absolutely have to.
    // Use getters and setters for external access to 
    // private or protected variables. That's what properties
    // are generally used for.

    // Basic get/set property
    public string Name 
    { 
        get { return name; } 
        set { name = value; } 
    } 

    // You can do read-only (get-only) properties too
    public Vector3 Position 
    {
        get { return position; }
    }

    // There's a nifty shorthand for read-only properties
    public Model3D Model => model;


    // Access modifiers work the same for methods
    public InitEntity( string n, Vector3 p = Vector3.zero )
    {
        name = n;
        position = p;

        ChangeModel( null );
    }

    // Note that this method is protected. It can only be 
    // called from this class and classes that inherit it.
    protected ChangeModel( Model3D m ) 
    {
        model = m;
        UpdateNameFromModel( model );
    }

    // Note that this method is private. It can only be
    // called in this class
    private UpdateNameFromModel( Model3D m ) 
    {
        name = m.modelName;
    }
}

// Classes can only derive from one other class in C#.
// If you need more functionality, you use an interface.
// If you need more, you use more interfaces.
public interface INetworkEntity
{
    // Note that methods of an interface are default
    // abstract (no definition) and public. We'll 
    // "implement" them in the class that inherits them.
    bool SendNetworkPacket( string data );
    bool ReceiveLoop();
}

// Now let's derive a class from Entity and INetworkEntity
// Player will be a derived class, Entity the superclass,
// and INetworkEntity an implemented interface.
public class Player : Entity, INetworkEntity
{
    private bool isConnectedToServer;

    // Player now has access to all the public and 
    // protected variables and functions of Entity, 
    // but not the private ones. Since INetworkEntity
    // is an interface, kind of like an abstract class,
    // we have to provide implementation for the methods.
    public bool SendNetworkPacket( string data )
    {
        if( !isConnectedToServer )
            return false;

        // Send a packet here...
    }

    public bool ReceiveLoop()
    {
        if( !isConnectedToServer ) 
            return false;

        // Loop here for received packets...
    }

    public bool ChangePlayerModel( Model3D m, Animator a)
    {
        // Note that this is legal. The method "ChangeModel"
        // is protected, and thereby accessible to Player.
        ChangeModel( m );
        
        // Also notice that we're calling a protected method
        // from a public one. Other classes can call 
        // ChangePlayerModel to change the player's model, 
        // which is a protected variable modified by a
        // protected method.

        // THIS IS NOT LEGAL! WE CAN'T ACCESS A PRIVATE
        // VARIABLE OF A SUPERCLASS!
        name = m.modelName; // Will not compile

        // Let's directly change a protected variable
        UpdateAnimator( a );
    }

    private bool UpdateAnimator( Animator a )
    {
        // We can do this, because the anim variable of
        // Entity is protected, so Player can access it.
        anim = a;
    }

    private bool StartGame()
    {
        // Of course, we can also directly modify public
        // variables... but so can everyone else.
        isActive = true;
        description = $"Player {Name}";
    }
}
Last edited by supersoup on 2022-01-07 10:38, edited 1 time in total.

arochester
Global Moderator
Global Moderator
Posts: 2140
Joined: 2010-12-07 19:55
Has thanked: 4 times
Been thanked: 11 times

Re: Doubt in a C# question.

#3 Post by arochester »

@supersoup

Be aware that you are answering a question that was asked 24th March 2021...

User avatar
supersoup
Posts: 3
Joined: 2022-01-07 03:50

Re: Doubt in a C# question.

#4 Post by supersoup »

arochester wrote: 2022-01-07 10:46 @supersoup

Be aware that you are answering a question that was asked 24th March 2021...
Ha, wasn't paying attention. Well, hopefully this is helpful to somebody in the present day.

Post Reply