Type Constructors
Type Constructors
TYPE CONSTRUCTORS
In modern languages, type constructors are provided to permit the programmer to define new domains. Each new domain may be, and normally is, bound to a new type name. The new type name can be used to declare objects that belong to the domain and to declare the domain of function parameters.
TYPE CONSTRUCTORS
is
CONT
used to construct new types from given ones. is used to initialize static data in a type. It is called by the common language runtime (CLR) before any instances of the type are created. Type constructors are static (Shared in Visual Basic) and cannot take parameters. is a keyword or syntactic construct whose use creates a new domain. Not all ways of declaring new types are type constructors. Type constructors are chosen by a language designer and vary greatly among languages.
TYPE CONSTRUCTORS
CONT
For example, array" and "" are type constructors in Pascal but the analogous [<dimension>]" and *" in C do not construct domains. However, struct in C and the analogous record in Pascal are both type constructors.
When a programmer uses a type constructor in a type declaration, the declared type name is bound to the newly formed domain. Thereafter, other program statements can declare objects and function parameters in that domain by referring to the type name directly or indirectly. In an ST#3 language, a domain, D, created by a type constructor is independent; that is, it is functionally incompatible with all existing or future domains. No functions may be applied to objects from D unless they are explicitly defined for D, and functions defined for D may not be applied to objects from any other domain. An object is type-compatible with a formal parameter if and only if both were declared to belong to the same domain.
Every occurrence of a type constructor constructs a different domain. If types D1 and D2 are declarations with identical type definitions containing a type constructor, D1 and D2 are incompatible. In some languages, a type constructor may be used in a variable or parameter.
TYPE BoxDimensions = array [1..3] of Real; Tank = BoxDimensions; VAR p, q: BoxDimensions; t: Tank; FUNCTION vol(d: BoxDimensions): real; BEGIN vol := d[1] * d[2] * d[3] END;
In some languages, a type defined by mapping does not construct a new, independent domain. Rather, the new type name is an alternative way to refer to an existing domain. The typedef declaration in C is an example. It defines the new type name as a synonym for its definition, which is often a structural description. Although typedef does not create a new domain, a typedef declaration is useful in C because the syntax for using typedef names is clearer and more convenient than the syntax for using their descriptions.
C was one of the earliest languages developed that permitted the programmer to declare new types. At that time, the relationships among the name of a type, its representation, and its semantics were only partially understood. This is probably why C has fewer type constructors than newer languages, and why typedef is not a type constructor.
EXAMPLES OF FUNCTION CALLS ON THREE PASCAL TYPES THAT BELONG TO THE SAME DOMAIN
TYPE LengthInFeet = Real; LengthInMeters = Real; VAR r: Real; f: LengthInFeet; m: LengthInMeters; FUNCTION FeetToMeters(f:LengthInFeet):LengthInMeters; BEGIN FeetToMeters := f * 12 * 0.00254 END;
The confusing connection among domains, type names, and representations was cleaned up" in Ada. The type structure in Ada is richer than either Pascal or C. Ada permits the programmer to declare a type D, mapped onto D, but choose whether D and D will be synonyms or name distinct domains. Ada provides two different declaration forms for using an old type to represent a new one. The first form, marked by the keyword subtype", creates a new name for part (or all) of the old domain. The names become synonyms as they would in the corresponding Pascal and C declarations.
CONSTRUCTORS IN JAVA
automatically called whenever an instance of the class is created. defined nearly the same way as how you define a method member of a class, with three exceptions: the name of the constructor must be the same name of the class. the constructor has an implicit return value, which is the class type. That is, you are not permitted to define a return value for a constructor. the constructor must be designated with any access specifier or no access specifier.
CALLING CONSTRUCTORS
At least two constructors are involved whenever a subclass inherits a superclass. Both of these classes have a constructor. Every class has a default constructor that is automatically called when you create an instance of a class. default constructor is defined for every class as part of the Java language. It has no arguments. Java calls the constructor of the subclass and the superclass when you declare an instance of the subclass. That is both constructors execute. Java calls the constructor of the superclass first and then calls the constructor of the subclass.
OVERLOADING A CONSTRUCTOR
class demo{ public static void main(String args[]){ rectangle mm=new rectangle(); rectangle nn=new rectangle(20); mm.print(); nn.print(); } } class rectangle{ private int length; public rectangle(){ length=10; } public rectangle(int l){ length=l; } public void print(){ System.out.println("The length is: "+length); }}
THANK YOU!!!