Reputation: 61755
Given:
myObject hello = new myObject();
What's the point of specifying myObject again in the second part of the line of code?
Would you ever do anything like:
myObject hello = new anotherObject();
Why not just
myObject hello = new();
Upvotes: 3
Views: 244
Reputation: 1899
Basic polymorphism. You can say myObject hello = new derivesFromMyObject().
Upvotes: 7
Reputation: 47058
You have the var
keyword
var hello = new myObject();
It is the same simplification, just on the other side.
The use comes when you have a class hierarchy.
class Base {}
class SubA : Base {}
class SubB : Base {}
Then you might want to have the variable declared as Base
even if you instantiate SubA
or SubB
The following would not work
SubA hello = new SubA();
hello = new SubB();
Upvotes: 1
Reputation: 9690
I've also wondered the same question.
In case we just don't use any kind of polymorphsm and subtyping, when we create and instanciate the object on the same line (like in your example), the A a = new(); syntax should be accepted...
We can use var keyword instead if we don't want repetition, but I don't like var on known types, it makes the code a little bit more complex to read.
Upvotes: 1
Reputation: 29642
Because this is how the C# language is defined.
The specification of the type on the left side doesn't have much to do with the specification of the type on the right side. The specification of the type on the left side specified the type of the variable. The specification of the type on the right side specifies the type you want to instantiate.
The var
keyword solves this issue, but the other way around from your example, so you can type:
var hello = new myObject();
Close enough I think.
Upvotes: 1
Reputation: 120997
First of all, the problem with stating the type twice has been solved, only the other way round (using the var
keyword) with:
var someClass = new SomeClass();
and yes, it is often useful to have a variable declared of another type than the actual instantiation, such as:
BaseClass b = new ChildClass();
or
MyInterface i = new ClassThatImplementsInterface();
Upvotes: 3
Reputation: 5474
myObject hello = new myObject();
The "myObject hello" declares the object.
The "new myObject()" calls the constructor for myObject... It's possible that you have several constructors that take different sets of parameters to create different flavors of the object.
Upvotes: 0
Reputation: 43094
Given the possibility of object inheritance/polymorphism then yes you may at some point do:
BaseObject A = new ChildObject();
Upvotes: 4
Reputation: 15335
Have you heard of polymorphism?
An assignment such as:
myObject hello = new();
would make it impossible, since you could not specify the actual type of the object being created.
You should not assume that the type of the variable and the type of the object that the variable points to are the same. That's the basis of polymorphism. For example:
Vehicle a = new Car();
Vehicle b = new Bike();
In most languages, the object's type must conform to the variable's type. In most OO languages, this means that the object's type must be the same type, a subtype, or an implementation (cfr. interfaces) of the variable's type.
Upvotes: 3
Reputation: 2925
Yes you can do
MyObject hello = new AnotherObject()
;
if AnotherObject is a subclass of MyObject.
Upvotes: 3