Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base Class = New Derived Class. How does this work?

Tags:

c#

class

Say I have a base class called Enemy and a derived class called Ogre.

What is the difference between creating an instance these two ways:

Enemy newOgre = new Ogre();

Ogre newOgre = new Ogre();
like image 237
GreenBandit Avatar asked Oct 14 '15 17:10

GreenBandit


People also ask

What happens when base and derived class?

[D]. Base class object will call base class function and derived class object will call derived class function.

Can a base class be derived from another class?

Base Class: A base class is a class in Object-Oriented Programming language, from which other classes are derived. The class which inherits the base class has all members of a base class as well as can also have some additional properties.

How base class and derived class related to each other?

A base class is an existing class from which the other classes are derived and inherit the methods and properties. A derived class is a class that is constructed from a base class or an existing class. 2. Base class can't acquire the methods and properties of the derived class.

How do you make a base class derived class?

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Constructors, destructors and copy constructors of the base class.


1 Answers

Actually, the piece of code that is creating the instance is only new Ogre(). What is in the left side of the equal sign has nothing to do with creating the instance.

The first statement is simply assigning the created instance to a variable of type Enemy. The second one is assigning it to a variable of type Ogre.

So you have two variables of different types pointing to objects of the same type, i.e. Ogre.

The variable (what is on the left side of the equal sign), only determines what you can access from the object. For example, if the Ogre class has a method that is not inherited from Enemy, then using the Enemy variable, you will not be able to access it.

Please note that the variable does not effect how the object behave. For example, if Ogre overrides a method defined in Enemy that does something different. Calling this method on an instance of Ogre using a variable of type Enemy would cause the overridden method in Ogre to be invoked, not the one in Enemy,

For example, consider these classes:

public class Enemy
{
    public virtual void Test()
    {
        Console.WriteLine("enemy");
    }
}

public class Ogre: Enemy
{
    public override void Test()
    {
        Console.WriteLine("ogre");
    }
}

Now if you do this:

Orge enemy = new Orge();
enemy.Test();

The console would print "ogre".

And if you do this:

Enemy enemy = new Ogre();
enemy.Test();

The console would still print "orge".

like image 126
Yacoub Massad Avatar answered Oct 11 '22 07:10

Yacoub Massad