What do you call the two types of classes on a instance declaration

When you declare a new instance of a class, you do so in a lot of languages as:

MyClass obj = new MyClass();

The two MyClass mean subtly different things, often you will see:

IMyClass obj = new MyClass();

where the LHS is an interface, or

BaseClass obj = new DerivedClass();

where the LHS is a base class from which the RHS inherits.

Is there a name for the two different uses (LHS and RHS) of classes in one declaration (e.g. declared class and instantiated class)?

The sentence I am trying to write is:

In Xamarin, SqlCommand.ExecuteReaderAsync(CancellationToken) throws a runtime error and so in the declaration, SqlCommand command = new SqlCommand(), the LHS Class must be changed to DbCommand.

3

You are declaring the type of the variable (static type) and then instantiate the object that the variable will hold (runtime type).

So, your sentence:

In Xamarin, SqlCommand.ExecuteReaderAsync(CancellationToken) throws a runtime error and so in the declaration, SqlCommand command = new SqlCommand(), the static type must be changed to DbCommand.

The two usages are actually completely unrelated.

The LHS is the declaration of the type of the variable obj. You can put any type there, not just a class. E.g. in both Java and C♯, you could put an interface there, in Java, you could also put a primitive type there, in C♯, you could put a struct there, in Scala, you could put a type alias there. Some languages have more sophisticated type languages, e.g. in Pike, you could put something like string | number there, which denotes the union type of either a string or a number.

On the RHS, you have a constructor call or instance creation expression. But again, it doesn’t have to be a constructor call: any expression e that is type-compatible with the variable declaration (i.e. e : Te <: Tvar) can be put there. For example, int i = 1; is perfectly valid, as is MyClass obj = methodCallThatReturnsAnInstanceOfASubclassOfMyClass();.

OTOH, instance creation expressions can appear anywhere an expression of (a supertype of) the instance creation expression’s type can appear. E.g. if you have a method void methodThatTakesMyClassAsArgument(MyClass foo), then you can call it with an instance creation expression as its argument: methodThatTakesMyClassAsArgument(new MyClass());.

The term you are looking for is type, and it isn’t related to classes at all.

Both of the type names in each declaration are just that: “types”. The left-hand type is the type of the variable, and the right-hand type is the type of the object you’re assigning to that variable (or initializing that variable with).

When the two types are different, in most languages that’s only possible if the object type is a subtype of the type of variable it’s being assigned to. In many cases, it’s natural to call the left-hand type the “abstract type” or the “interface” and the right-hand type the “concrete type“.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *