-5

Can someone please explain what is the difference between

ParentClass obj= new ChildChildClass();     

and

ParentClass obj= new ParentClass();

Since both

class ParentClass{
   public void ParentClassMethod(){
   }
}

class ChildClass : ParentClass{
   public void ChildClassMethod(){
   }
}

As per my understanding what is accessible remains same in both cases. So what is the point?

Borgleader
  • 15,826
  • 5
  • 46
  • 62
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • You would see a difference if the base class was `abstract` or had any `virtual` methods. In this concrete case, there would be no difference, except that using the derived (child) class as the variable type would allow you to access its child methods too (and potentially be able to pass it to different, more specific methods). – vgru Dec 14 '15 at 16:27
  • You can cast the first to a ChildClass and thus access ChildClass methods and properties. – stuartd Dec 14 '15 at 16:28
  • 1
    Did you try searching? Plenty of Q&As handling polymorphism. It becomes obvious once you introduce another type deriving from `ParentClass`. – CodeCaster Dec 14 '15 at 16:29
  • @CodeCaster yes i did. – SamuraiJack Dec 14 '15 at 16:29
  • 1
    And what was the result of that search? – CodeCaster Dec 14 '15 at 16:30
  • @CodeCaster they are not the same. like for ex: http://stackoverflow.com/questions/15596193/whats-the-difference-between-subclass-sc-new-subclass-and-superclass-sc-ne?lq=1 – SamuraiJack Dec 14 '15 at 16:30
  • 1
    Yes, so what is your question? _"What's the difference"_ isn't really answerable, other than _"You're instantiating two different classes"_. – CodeCaster Dec 14 '15 at 16:31
  • @CodeCaster this question might idiotic to you but trust me there are plenty others like me who are confused by this question. Not everybody is experienced like you. – SamuraiJack Dec 14 '15 at 16:33
  • I'm not saying it is idiotic, I'm genuinely interested in what kind of answers you're looking for. It is hard to give a proper answer without having to dive into what variables are and how polymorphism works, and all of that is very well findable on the web. – CodeCaster Dec 14 '15 at 16:35
  • @CodeCaster My question is simple. What is the difference? Are there any cases when you would prefer one over the other? A little bit of explanation would be helpful. I dont know why this question is attracting so many downvotes. – SamuraiJack Dec 14 '15 at 16:36
  • And I'm trying to point out that _"What's the difference"_ is an uninteresting, or at least the wrong question. The difference is that you are instantiating two different classes, period. Now what? See [Creating variable of type to store object in C#](http://stackoverflow.com/questions/23172380/creating-variable-of-type-base-class-to-store-derived-class-object-in-c-shar) for a related question. – CodeCaster Dec 14 '15 at 16:37

4 Answers4

1

You're instantiating two entirely different objects (that both happen to be, at some level, a ParentClass). Though the code will now see them as the same type, the underlying object will be different.

I could take the same idea and make it more obvious:

object o = "Hello World!";
object o2 = 15;

string and int are both objects, so this is fine. However the underlying type is certainly different here.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Ok so in my first case I am creating an object of ChildClass. But I can access only those members which it has derived from its base class. And in second case I am creating an object of ParentClass, so naturally I can access only the members which are in that class. Am I right? – SamuraiJack Dec 14 '15 at 16:45
1

You can override virtual methods. A simple example:

void Main()
{
  A realA = new A();
  A actuallyB = new B();

  Console.WriteLine(realA.GetName()); // John
  Console.WriteLine(actuallyB.GetName()); // Gillian

  PrintName(realA); // John
  PrintName(actuallyB); // Gillian
}

public void PrintName(A anyA)
{
  Console.WriteLine(anyA.GetName());
}

public class A
{
  public virtual string GetName() 
  {
    return "John";
  }
}

public class B : A
{
  public override string GetName() 
  {
    return "Gillian";
  }
}

Your two classes can have different behaviour (within the bounds of Liskov Substitution Principle, of course), but the helper method PrintName will work with both.

In fact, this is the whole reason for the existence of inheritance - you expose some useful interface, and everyone who's using it can treat it one way. If you want a more real world example, let's look at the Stream class. For example, you might have a helper method that reads an image from a stream. Now, when you need to read an image from a file, you just pass a file stream to your helper method. When you want to read an image from a database, you just pass a database stream. When you want to read an image from a web server... As long as the streams conform to the stream interface (and the truth is, streams aren't exactly perfectly designed ;)), you can switch them at will, and everything will work as expected.

Luaan
  • 62,244
  • 7
  • 97
  • 116
0

It is quite difficult to say like "The difference is in..."

It is one of the OOP principles called Inheritance. It means that ChildClass at the same time is ParentClass. So you can store both this types in collection of type ParentType or to pass as a parameter. But to access child's method you need to cast to child's type.

Alex Lebedev
  • 601
  • 5
  • 14
0
ParentClass obj = new ChildChildClass(); 

Here obj is actually a ChildChildClass and at any time you can cast it back to ParentClass and call the methods available in ParentClass.

The advantage for such usages is you can have some common functionality which works with ParentClass only and there you want to pass ChildChildClass also as it is also a ParentClass.

Morbia
  • 4,144
  • 3
  • 21
  • 13