Difference between revisions of "Instantiation"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Blanked the page)
(Instantiation)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
=Instantiation=
 +
:Instantiation is the process of allocating a block of memory to a new object instance. Instantiation is the direct process of declaring a new object by ClassName, giving it an identifier Name, and in some cases passing initialization arguments to the new object. For Example:
  
 +
==Examples==
 +
For a class Based Example Look Below:
 +
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
 +
 +
// Class Car Inherits from base Object
 +
// (Reference To Object Class Unnecessary)
 +
class Car(Object) {
 +
    // No Parameter Constructor
 +
    public Car() {
 +
        this.licenseNumber = "L-NUM";
 +
        this.age = 0;
 +
    }
 +
 +
    // Parameter Constructor
 +
    public Car(String licenseNumber, int Age) {
 +
        this.licenseNumber = licenseNumber;
 +
        this.age = Age
 +
    }
 +
 +
    public void Output() {
 +
        Console.WriteLine("Car With Plate {0} Is {1} Years Old", this.licenseNumber, this.age)
 +
    }
 +
 +
    public String licenseNumber;
 +
    public int age;
 +
}
 +
 +
Car myCar1 = new Car() // Initializes new Car Instance Without Passing Any Arguments To Constructor
 +
Car myCar2 = new Car("MyLicenseNumber", 4) // Initializes new Car Instance by Passing arguments To Constructor
 +
 +
myCar1.Output(); // Outputs "Car With Plate L-Num Is 0 Years Old"
 +
myCar2.Output(); // Outputs "Car With Plate MyLicenseNumber Is 5 Years Old"
 +
</syntaxhighlight>
 +
</tabber>
 +
 +
:The Example Above Demonstrates the process of object initialization, via separate functionally overloaded constructors. Both Forms of Initializing an Instance Of Class Car, are Viable and result in the same block of memory Being Allocated To Both Instances; the only difference between the two instances are the values of the instance members with one leaving instance members as their default values (as defined in the default parameter less base constructor) and the other setting the values of the instance members during initialization instead of afterwards using 'dot' notation. Lastly The following example illustrates the irrelevance of static instance members to object initialization, and the ability to take advantage of static members to add functionality to classes.
 +
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
 +
 +
// Class Animal Inherits from base Object
 +
// (Reference To Object Class Unnecessary)
 +
Class Animal(Object) {
 +
    public Animal(String animalName, int numOfLegs) {
 +
        this.animalName = animalName;
 +
        this.numOfLegs = numOfLegs;
 +
        Animal._animalCount += 1;
 +
        // Increments Count Of Animals
 +
        // Every Time Constructor Is Called
 +
    }
 +
 +
    public static void OutputACount() {
 +
        Console.WriteLine("Animal Count : {0}", Animal.animalCount);
 +
    }
 +
 +
    public String animalName;
 +
    public int numOfLegs;
 +
   
 +
    private static int _animalCount;
 +
    public static int animalCount { get return _animalCount; };
 +
}
 +
 +
Animal an1 = new Animal("Chuhaha", 4);
 +
Animal an2 = new Animal("Monkey", 2);
 +
 +
Animal.OutputACount(); // Outputs 2
 +
Animal.OutputACount(); // Outputs 2
 +
 +
Animal an3 = new Animal("Neanderthal", 2);
 +
Animal.OutputACount(); // Outputs 3
 +
 +
</syntaxhighlight>
 +
</tabber>
 +
 +
:The Above Example Shows That Regardless Of How An Instance Is Initialized, Static members are unaffected unless expressly set to; Hence a static instance member unlike a constant value, can be changed during every initialization of a given class (into an object), regardless of a given users intent towards that value (such as forcefully setting animalCount to new values, which in this case will just produce an error).

Latest revision as of 12:48, 29 June 2018

Instantiation

Instantiation is the process of allocating a block of memory to a new object instance. Instantiation is the direct process of declaring a new object by ClassName, giving it an identifier Name, and in some cases passing initialization arguments to the new object. For Example:

Examples

For a class Based Example Look Below:

 1 // Class Car Inherits from base Object 
 2 // (Reference To Object Class Unnecessary)
 3 class Car(Object) {
 4     // No Parameter Constructor
 5     public Car() {
 6         this.licenseNumber = "L-NUM";
 7         this.age = 0;
 8     }
 9 
10     // Parameter Constructor
11     public Car(String licenseNumber, int Age) {
12         this.licenseNumber = licenseNumber;
13         this.age = Age
14     }
15 
16     public void Output() {
17         Console.WriteLine("Car With Plate {0} Is {1} Years Old", this.licenseNumber, this.age)
18     }
19 
20     public String licenseNumber;
21     public int age;
22 }
23 
24 Car myCar1 = new Car() // Initializes new Car Instance Without Passing Any Arguments To Constructor
25 Car myCar2 = new Car("MyLicenseNumber", 4) // Initializes new Car Instance by Passing arguments To Constructor
26 
27 myCar1.Output(); // Outputs "Car With Plate L-Num Is 0 Years Old"
28 myCar2.Output(); // Outputs "Car With Plate MyLicenseNumber Is 5 Years Old"

The Example Above Demonstrates the process of object initialization, via separate functionally overloaded constructors. Both Forms of Initializing an Instance Of Class Car, are Viable and result in the same block of memory Being Allocated To Both Instances; the only difference between the two instances are the values of the instance members with one leaving instance members as their default values (as defined in the default parameter less base constructor) and the other setting the values of the instance members during initialization instead of afterwards using 'dot' notation. Lastly The following example illustrates the irrelevance of static instance members to object initialization, and the ability to take advantage of static members to add functionality to classes.

 1 // Class Animal Inherits from base Object 
 2 // (Reference To Object Class Unnecessary)
 3 Class Animal(Object) {
 4     public Animal(String animalName, int numOfLegs) {
 5         this.animalName = animalName;
 6         this.numOfLegs = numOfLegs;
 7         Animal._animalCount += 1;
 8         // Increments Count Of Animals
 9         // Every Time Constructor Is Called
10     }
11 
12     public static void OutputACount() {
13         Console.WriteLine("Animal Count : {0}", Animal.animalCount);
14     }
15 
16     public String animalName;
17     public int numOfLegs;
18     
19     private static int _animalCount;
20     public static int animalCount { get return _animalCount; };
21 }
22 
23 Animal an1 = new Animal("Chuhaha", 4);
24 Animal an2 = new Animal("Monkey", 2);
25 
26 Animal.OutputACount(); // Outputs 2
27 Animal.OutputACount(); // Outputs 2
28 
29 Animal an3 = new Animal("Neanderthal", 2);
30 Animal.OutputACount(); // Outputs 3

The Above Example Shows That Regardless Of How An Instance Is Initialized, Static members are unaffected unless expressly set to; Hence a static instance member unlike a constant value, can be changed during every initialization of a given class (into an object), regardless of a given users intent towards that value (such as forcefully setting animalCount to new values, which in this case will just produce an error).