Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
BaseClass obj1 = new BaseClass();
BaseClass obj2 = new ChildClass();

what makes the obj2 special over obj1?
if there is any scenario which can make obj2 special please quote the same.

What I have tried:

I have tried initializing object in both ways and using methods, parameters from it both looks similar only for me.
Posted
Updated 13-Mar-18 3:46am

A "child" class inherits it's base class' public/protected components, and can provide additional functionality via its own properties and methods.

The topic of inheritance is very broad, and you should probably find a reference using google.
 
Share this answer
 
Quote:
what makes the obj2 special over obj1?
Nothing.
In fact you can do this:
C#
BaseClass obj1 = new BaseClass();
BaseClass obj2 = new ChildClass();
obj1 = obj2;

What's happening is inheritance - ChildClass inherits from BaseClass, so it is "Base class with extras" - every instance is both a ChildClass instance and a BaseClass instance at the same time.

Think about cars for a moment: You can drive a Car. Which means you can drive a Ford, and a Mercedes; and further you can drive a Ford Fiesta, and a Ford Focus, and a Mercedes A Class, and a Bugatti Veyron - you don't have to take a new test for each manufacturer or each model.
"Focus" inherits from "Ford", which inherits from "Car"
"A Class" inherits from "Mercedes", which inherits from "Car".

So anything that a Car can do (StartTheEngine, DriveToTheShops) any Ford can do, and any Focus or Fiesta can also do.

Back to computers, and it's the same thing: You have a base class and all derived classes can do everything that the base can. When you use the variable, it's type determines what the compiler will let you do:
C#
BaseClass obj1 = new BaseClass();
BaseClass obj2 = new ChildClass();
obj1.BaseClassMethod();
obj2.BaseClassMethod();
All perfectly fine.
But it won't let you use ChildClass specific items:
C#
obj1.ChildClassMethod();
obj2.ChildClassMethod();
Both will give you a compiler error because obj1 and obj2 are both capable of holding a BaseClass which doesn't mean that what they hold will be a ChildClass instance.

Does that make any sense?
 
Share this answer
 
Classes

public class Cat : Animal
{
    public int NumberOfBirdsKilled { get; set; }
    public override string Speak()
    {
        return "Meow";
    }
}

public class Dog : Animal
{
    public int NumberOfButtsSniffed { get; set; }
    public override string Speak()
    {
        return "Woof";
    }
}

public class SockMaker
{
    public void MakeSocks(Animal animal)
    {
        for (int i = 1; i <= animal.NumberOfLegs; i++)
        {
            Console.WriteLine("Making sock number " + i.ToString());
        }
    }
}


Animal obj1 = new Animal();
Animal obj2 = new Cat();

// obj1 references an instance of the base class Animal and that instance only has Animal properies like NumberOfLegs or Speak()
// the obj1 variable is of base type Animal so only allows access to Animal properties like NumberOfLegs or Speak()
obj1.NumberOfLegs = 8;

// obj2 references an instance of the child class Cat and that instance has both Animal properties and Cat properties
// the obj2 variable is of base type Animal so only allows access to Animal properties like NumberOfLegs or Speak()

obj2.NumberOfLegs = 4;
// obj2.NumberOfBirdsKilled = 90001; <-- won't compile

// the above line doesn't compile because even though the object referenced by obj2 has a NumberOfBirdsKilled property
// the obj2 variable itself only knows about Animal properties and that is a Cat property, not an Animal one
// so the object referenced by the variable obj2 has properties that we can't access even though they do exist
// contrast that with obj1 where the object referenced is Animal and the reference variable is also Animal
// so we can access all possible properties of the object, nothing is blocked off

// because obj2 references a Cat object we can "up cast" it to Cat

Cat c = (Cat)obj2;

// both "c" and "obj2" reference the same object but because the "c" variable is type Cat it knows about both Cat
// and Animal properties so can access both

Console.WriteLine(c.NumberOfLegs); // this will write "4", remember there is only one Cat object that both obj2 and c reference
c.NumberOfBirdsKilled = 9001; // this property always existed on the object, but now we can access it as we are using a variable of type Cat

// why would we want to do this?  Let's say we have a class that only cares about the properties on Animal
List<Animal> animals = new List<Animal>();

animals.Add(new Cat { NumberOfLegs = 4, NumberOfBirdsKilled = 9001 });
animals.Add(new Dog { NumberOfLegs = 4, NumberOfButtsSniffed = 7 });

SockMaker sm = new SockMaker();

foreach (Animal a in animals)
{
    // "a" might reference a "Cat", might be a "Dog", the sock maker doesn't care as it is only interested
    // in the NumberOfLegs property so only needs a variable type that can access that property
    sm.MakeSocks(a);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900