Now, instances of various packet objects can be created and put into the array: EtherPacket ep = new; // extends BasePacket TokenPacket tp = new; // extends BasePacket GPSSPacket gp = new; // extends EtherPacket packets = ep; packets = tp; packets = gp; If the data types were, for example, integers, bits, and strings, all of these types could not be stored into a single array, but with polymorphism, it can be done. In this example, because the methods were declared as virtual, the appropriate subclass methods can be accessed from the superclass variable, even though the compiler did not know—at compile time—what was going to be loaded into it. For example, packets packets.send(); shall invoke the send method associated with the TokenPacket class. At run time, the system correctly binds the method from the appropriate class. This is a typical example of polymorphism at work, providing capabilities that are far more powerful than what is found in a nonobject-oriented framework. 11. What is the use of the abstract class? EXAMPLE: useing abstract class
virtual class A ; virtual task disp (); $display ( " This is class A " ); endtask endclass class EA extends A ; task disp (); $display ( " This is Extended class A " ); endtask endclass program main ; EA my_ea ; A my_a ; initial begin my_ea = new (); my_a = my_ea ; my_ea . disp (); my_a . disp (); end endprogram RESULT This is Extended class A This is Extended class A EXAMPLE: creating object of virtual class virtual class A ; virtual task disp (); $display ( " This is class A " ); endtask endclass program main ; A my_a ; initial begin my_a = new (); my_a . disp (); end
endprogram RESULT Abstract class A cannot be instantiated Virtual keyword is used to express the fact that derived classes must redefine the properties to fulfill the desired functionality. Thus from the abstract class point of view, the properties are only specified but not fully defined. The full definition including the semantics of the properties must be provided by derived classes. Definition (Abstract Class) A class A is called abstract class if it is only used as a superclass for other classes. Class A only specifies properties. It is not used to create objects. Derived classes must define the properties of A.
You've reached the end of your free preview.
Want to read all 22 pages?
- Fall '19
- Object-Oriented Programming, SystemVerilog