Difference between new() and upcasting?

After course on SystemVerilog OOP, I made 2 examples to figure the upcast out. What is the difference b1 = new(); and b1 = e1;?
There’s nothing different on the result. Why do we need upcast instead of using new()?

1.Exampple of “b1=new()”.



ackage test_classes;                                                    
                                                                         
 class base;                                                             
      bit [7:0] a;                                                       
      bit [31:0] addr;                                                   
                                                                         
      function display();                                                
        $display("Addr = %0d",addr);                                     
      endfunction                                                        
                                                                         
                                                                         
      function void showit();                                            
        $display("BASE(%m) : a = %2d", a);                               
      endfunction                                                        
                                                                         
      function void seta( bit [7:0] din);                                
        a=din;                                                           
      endfunction                                                        
                                                                         
      function bit [7:0] geta();                                         
        return a;                                                        
      endfunction                                                        
    endclass                                                             
                                                                         
  class ext extends base;                                                
      bit [7:0] data;                                                    
      bit [7:0] b;                                                       
                                                                         
      function display();                                                
      //super.display();                                                 
      $display("Data = %0d",data);                                       
      endfunction                                                        
                                                                         
      function void showit();                                            
        $display("EXT(%m) : data= %2d  a=%2d ", data, b  );              
      endfunction                                                        
                                                                         
      function void setdata(bit [7:0] din);                              
        data = din;                                                      
      endfunction                                                        
                                                                         
      function bit [7:0] getdata();                                      
        return data;                                                     
      endfunction                                                        
    endclass                                                             
endpackage
module test;                                                             
  import test_classes::*;                                                
  base    b1, b2;                                                        
  ext     e1, e2;                                                        
  bit     e1good, e2good;                                                
  bit [7:0] m_data;                                                      
                                                                         
  initial begin                                                          
  e1 =new();                                                             
                                                                         
  e1.data = 3;                                                           
  e1.b = 1;                                                              
  e1.setdata(7);                                                         
  e1.getdata();                                                          
  e1.showit();                                                           
                                                                         
  b1 = new();                                                          
//  b1= e1;                                                                
                                                                         
  b1.a = 8;                                                              
  b1.showit();
  end
endmodule

2.Exampple of “b1=e1”.



ackage test_classes;                                                    
                                                                         
 class base;                                                             
      bit [7:0] a;                                                       
      bit [31:0] addr;                                                   
                                                                         
      function display();                                                
        $display("Addr = %0d",addr);                                     
      endfunction                                                        
                                                                         
                                                                         
      function void showit();                                            
        $display("BASE(%m) : a = %2d", a);                               
      endfunction                                                        
                                                                         
      function void seta( bit [7:0] din);                                
        a=din;                                                           
      endfunction                                                        
                                                                         
      function bit [7:0] geta();                                         
        return a;                                                        
      endfunction                                                        
    endclass                                                             
                                                                         
  class ext extends base;                                                
      bit [7:0] data;                                                    
      bit [7:0] b;                                                       
                                                                         
      function display();                                                
      //super.display();                                                 
      $display("Data = %0d",data);                                       
      endfunction                                                        
                                                                         
      function void showit();                                            
        $display("EXT(%m) : data= %2d  a=%2d ", data, b  );              
      endfunction                                                        
                                                                         
      function void setdata(bit [7:0] din);                              
        data = din;                                                      
      endfunction                                                        
                                                                         
      function bit [7:0] getdata();                                      
        return data;                                                     
      endfunction                                                        
    endclass                                                             
endpackage
module test;                                                             
  import test_classes::*;                                                
  base    b1, b2;                                                        
  ext     e1, e2;                                                        
  bit     e1good, e2good;                                                
  bit [7:0] m_data;                                                      
                                                                         
  initial begin                                                          
  e1 =new();                                                             
                                                                         
  e1.data = 3;                                                           
  e1.b = 1;                                                              
  e1.setdata(7);                                                         
  e1.getdata();                                                          
  e1.showit();                                                           
                                                                         
//  b1 = new();                                                          
  b1= e1;                                                                
                                                                         
  b1.a = 8;                                                              
  b1.showit();
  end
endmodule

In reply to UVM_LOVE:

There are no visible differences in what your two examples display. However, the first example constructs one ext object, and the second example constructs two objects: a base and ext object. If you add
$display(b1.a, e1.a);
to the end of the
initial
block or make
showit
a virtual function, you will see the difference.

In reply to dave_59:

I can see the difference between after upcasting as your recommand… Thank you dave_59.
But still it does not clear for me with one OOP Polymorphism example.

The common usage of upcasting is that the main idea is I can call 2 different way by upcasting.
To see the usage of upcasting, I made one example(sorry for my wrong code).
But, If I ran my example, I just got

BASE(test_classes::base.showit) : a = 0
BASE(test_classes::base.showit) : a = 0

Here I’m confused that I ran different object(e1, e2) but same result I have.
I can’t see the features of upcasting by the using Passing object as function argument.
What is the usage of Upcasting for OOP Polymorphism example?

I can’t connection between the former and latter example about Upcating with OOP Polymorphism. How do I call 2 different way by upcating correctly?


package test_classes;

 class base;
      bit [7:0] a;
      bit [31:0] addr;

      virtual function display();
        $display("Addr = %0d",addr);
      endfunction

      function void showit();
        $display("BASE(%m) : a = %2d", a);
      endfunction

      function void seta( bit [7:0] din);
        a=din;
      endfunction

      function bit [7:0] geta();
        return a;
      endfunction
    endclass

  class ext extends base;
      bit [7:0] data;
      bit [7:0] b;

      function display();
      $display("EXT Data = %0d",data);
      endfunction

      function void showit();
        $display("EXT(%m) : data= %2d  a=%2d ", data, b  );              
      endfunction                                                        
 
      function void setdata(bit [7:0] din);                              
        data = din;                                                      
      endfunction
     
 
      function bit [7:0] getdata();                                      
        return data;                                                     
      endfunction                                                        
    endclass

  class ext2 extends base;
      bit [7:0] data;
      bit [7:0] b;

      function display();
      $display("EXT2 Data = %0d",data);
      endfunction
	        function void showit();
        $display("EXT2(%m) : data= %2d  a=%2d ", data, b  );
      endfunction

      function void setdata(bit [7:0] din);
        data = din;
      endfunction


      function bit [7:0] getdata();
        return data;
      endfunction
    endclass

endpackage

module test;
  import test_classes::*;

  initial begin
    base    m_seq[$];
    ext     e1 = new();
    ext2    e2 = new();

    m_seq.push_back(e1);
    m_seq.push_back(e2);

    foreach (m_seq[i]) begin
      m_seq[i].showit();
    end
  end
endmodule