title: Delphi Type Casting
body: <p>I need a clarification on Delphi Type Casting<br>
  I wrote an example with two classes: TClassA and TClassB, TClassB derived from TClassA.<br></p>
  
  <p>the code is as follows:<br></p>
  
  <pre><code>program TEST;
  
  {$APPTYPE CONSOLE}
  
  
  uses
    System.SysUtils;
  
  type
    TClassA = class(TObject)
    public
      Member1:Integer;
      constructor Create();
      function ToString():String; override;
    end;
  
  type
    TClassB = class(TClassA)
    public
      Member2:Integer;
      constructor Create();
      function ToString():String; override;
      function MyToString():String;
    end;
  
  { TClassA }
  
  constructor TClassA.Create;
  begin
    Member1 := 0;
  end;
  
  function TClassA.ToString: String;
  begin
    Result := IntToStr(Member1);
  end;
  
  { TClassB }
  
  constructor TClassB.Create;
  begin
    Member1 := 0;
    Member2 := 10;
  end;
  
  function TClassB.MyToString: String;
  begin
    Result := Format('My Values is: %u AND %u',[Member1,Member2]);
  end;
  
  function TClassB.ToString: String;
  begin
    Result := IntToStr(Member1) + ' - ' + IntToStr(Member2);
  end;
  
  
  procedure ShowInstances();
  var
    a: TClassA;
    b: TClassB;
  begin
    a := TClassA.Create;
    b := TClassB(a); // Casting (B and A point to the same Memory Address)
    b.Member1 := 5;
    b.Member2 := 150; // why no error? (1)
  
    Writeln(Format('ToString: a = %s, a = %s',[a.ToString,b.ToString])); // (2)
    Writeln(Format('Class Name: a=%s, b=%s',[a.ClassName,b.ClassName])); // (3)
    Writeln(Format('Address: a=%p, b=%p',[@a,@b])); // (4)
    Writeln(b.MyToString); // why no error? (5)
  
    readln;
  end;
  
  begin
    try
      ShowInstances;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  end.
  </code></pre>
  
  <p>The program output is:</p>
  
  <pre><code>ToString: a = 5, a = 5
  Class Name: a=TClassA, b=TClassA
  Address: a=0012FF44, b=0012FF40
  My Values is: 5 AND 150
  </code></pre>
  
  <p>(1) what's the Member2 address? is this a possible "Access Violation"?<br>
  (2) ok, ToString() method point to the same address<br>
  (3) why a and b have the same ClassName?<br>
  (4) ok, a and b are two different variables<br>
  (5) if b is TClassA, why can use "MyToString" method?<br></p>