1

Suppose we have a class ProductA that inherited another class called Product. What's the meaning of

Product product = new ProductA();

And what's the difference between that and

ProductA product = new ProductA();
CharithJ
  • 46,289
  • 20
  • 116
  • 131
palapapa
  • 573
  • 2
  • 5
  • 25
  • In your concrete example - no difference. Except that 'new' modifier on later methods will override behavior in second example, but you should not ever use it, this is just another garbage from world of legacy. – eocron Feb 19 '20 at 07:13
  • Then under what circumstances can these two syntaxes have a difference? – palapapa Feb 19 '20 at 07:16
  • @eocron We are talking about construction of an instance and assign that instance to a variable. – Sir Rufo Feb 19 '20 at 07:17
  • This goes way beyond a constructor issue, @SirRufo. a base class and it's descendants can be vastly different things (e.g. Hardware and Printer). – LongChalk Feb 19 '20 at 07:27
  • 1
    @palapapa, if you send this object to a method, say public void DoSomething(ProductA productA) { ...} then the first object will create a compile time error, and the second won't. – LongChalk Feb 19 '20 at 07:48

3 Answers3

3

When initializing, no difference. It is just like saying "ProductA is a Product type of thing".

In object-oriented programming, the concept of IS-A is a based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance.

Then under what circumstances can these two syntaxes have a difference?

Why to use Polymorphism?

Why and when use polymorphism?

Additional reading : The difference between virtual, override, new and sealed override

CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • Generally, I agree with you, BUT _No Difference_ is not always true. If a subclass has more functionality rather than base class, in the first example, he missed this functionality. Or If subclass base on bad code, hide an inheriented member, they will have the difference. – Hamed Moghadasi Feb 19 '20 at 07:29
  • There will be no difference in the **runtime** instance create (e.g. they will both return the same product.GetType() in reflection). There will be a big difference in terms of how product is treated in the code (e.g. typeof(Product) and typeof(ProductA) are different). – LongChalk Feb 19 '20 at 07:54
3

There's no difference in the terms of what instance gets created, but there is a difference. In your first example, you can only access members defined by Product.

This is similar to the following.

object x1 = "hello";
string x2 = "hello";

In both cases a string instance is created, but you can call .Length on x2, but not on x1.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
1

A big difference. A context difference. A generality difference. Let me give you a simple example

public class Animal {
 ...
}

and

public class Dog : Animal {
...
}

then if you have a Animal dog = new Dog(); that's not going to expose (in the compile time realm) any additional properties the dog has over the animal. your "dog" is just an animal and as such it should be treated. nothing additional will appear in intellisense auto complete for example. All you want to "know" when you are using this object is therefor limited to it being an animal. if you have a Dog dog = new Dog(); you want (in the compile time realm) to treat it like a dog. It will expose it's full functionality and properties. A dog has a tail, and an animal doesn't for example. Do you want to use "tail" property in your code?

Also suppose you have a DogTender with a TakeCareOfDog(Dog myDoggy) method. If you stated Animal dog = new Dog(); well than the instance of DogTender can't handle your dog (because it's not a dog. It's an animal). However, your animal can fit well in an AnimalCollection (like a Zoo). We call this the Liskov principle or the Substitution principle - the decendant classes tell their base class - "anything you can do, I can do better". Anything an Animal can do, a dog can also do, but not the other way around.

LongChalk
  • 783
  • 8
  • 13