Vous êtes sur la page 1sur 6

WWW.TESTBENCH.

INSystemverilogOOPS

http://www.testbench.in/CL_05_INHERITANCE.html

|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |

Search

TUTORIALS SystemVerilog Verification Constructs Interface OOPS Randomization Functional Coverage Assertion DPI UVM Tutorial VMM Tutorial OVM Tutorial Easy Labs : SV Easy Labs : UVM Easy Labs : OVM Easy Labs : VMM AVM Switch TB VMM Ethernet sample

INHERITANCE
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. Definition (Inheritance) Inheritance is the mechanism which allows a class A to inherit properties of a class B. We say ``A inherits from B''. Objects of class A thus have access to attributes and methods of class B without the need to redefine them. The following definition defines two terms with which we are able to refer to participating classes when they use inheritance. Definition (Superclass) If class A inherits from class B, then B is called superclass of A. Definition (Subclass) If class A inherits from class B, then A is called subclass of B.

Objects of a subclass can be used where objects of the corresponding superclass are expected. This is due to the fact that objects of the subclass share the same behaviour as objects of the superclass. Superclasses are also called parent classes. Subclasses may also be called child classes or extended classes or just derived classes . Of course, you can again inherit from a subclass, making this class the superclass of the new subclass. This leads to a hierarchy of superclass/subclass relationships.

Index Introduction Class Object This Inheritance Encapsulation Polymorphism Abstract Classes Parameterised Class Nested Classes Constant Static Casting Copy Scope Resolution Operator Null External Declaration Classes And Structures Typedef Class Pure Other Oops Features Misc

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can Report a Bug or derive your new class from the existing class. In doing this, you can reuse the fields and Comment on This section - Your input is methods of the existing class without having to write (and debug!) them yourself. what keeps Testbench.in A subclass inherits all the members (fields, methods, and nested classes) from its improving with time! superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Verilog Verification Verilog Switch TB Basic Constructs

What You Can Do In A Subclass: A subclass inherits all of the public and protected members of its parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members: The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.

OpenVera Constructs Switch TB RVM Switch TB RVM Ethernet sample

Specman E Interview Questions

1of6

7/4/20128:44PM

WWW.TESTBENCH.INSystemverilogOOPS

http://www.testbench.in/CL_05_INHERITANCE.html

You can declare new methods in the subclass that are not in the superclass. You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Overriding Following example shows, adding new varibles, new methods, redefining existing methods.

EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); $display(" THIS IS SUBCLASS "); endtask endclass program main; initial begin parent p; subclass s; p = new(); s = new(); p.printf(); s.printf(); end endprogram RESULT THIS IS PARENT CLASS THIS IS SUBCLASS

Super The super keyword is used from within a derived class to refer to members of the parent class. It is necessary to use super to access members of a parent class when those members are overridden by the derived class. EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); super.printf(); endtask endclass program main;

2of6

7/4/20128:44PM

WWW.TESTBENCH.INSystemverilogOOPS
initial begin subclass s; s = new(); s.printf(); end endprogram RESULT THIS IS PARENT CLASS

http://www.testbench.in/CL_05_INHERITANCE.html

The member can be a member declared a level up or be inherited by the class one level up. There is no way to reach higher (for example, super.super.count is not allowed). Subclasses (or derived classes) are classes that are extensions of the current class whereas superclasses (parent classes or base classes) are classes from which the current class is extended, beginning with the original base class. NOTE: When using the super within new, super.new shall be the first statement executed in the constructor. This is because the superclass must be initialized before the current class and, if the user code does not provide an initialization, the compiler shall insert a call to super.new automatically.

Is Only Method Programmers can override the existing code/functionality before existing code and replaces with new code as shown in below example. EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); $display(" THIS IS SUBCLASS "); endtask endclass program main; initial begin subclass s; s = new(); s.printf(); end endprogram RESULT: THIS IS SUBCLASS

Is First Method Programmers can add new lines of code/functionality before existing code as shown in below example.

3of6

7/4/20128:44PM

WWW.TESTBENCH.INSystemverilogOOPS
EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); $display(" THIS IS SUBCLASS "); super.printf(); endtask endclass program main; initial begin subclass s; s = new(); s.printf(); end endprogram RESULT: THIS IS SUBCLASS THIS IS PARENT CLASS

http://www.testbench.in/CL_05_INHERITANCE.html

Is Also Method Programmers can add new lines of code/functionality after the existing code as shown in below example. EXAMPLE: class parent; task printf(); $display(" THIS IS PARENT CLASS "); endtask endclass class subclass extends parent; task printf(); super.printf(); $display(" THIS IS SUBCLASS "); endtask endclass program main; initial begin subclass s; s = new(); s.printf(); end endprogram RESULT: THIS IS PARENT CLASS THIS IS SUBCLASS

Overriding Constraints.

4of6

7/4/20128:44PM

WWW.TESTBENCH.INSystemverilogOOPS

http://www.testbench.in/CL_05_INHERITANCE.html

Programmers can override the existing constraint and replaces with new constraint as shown in below example. EXAMPLE: class Base; rand integer Var; constraint range { Var < 100 ; Var > 0 ;} endclass class Extended extends Base; constraint range { Var < 100 ; Var > 50 ;} // Overrighting the Base class constraints. endclass program inhe_31; Extended obj; initial begin obj = new(); for(int i=0 ; i < 100 ; i++) if(obj.randomize()) $display(" Randomization sucsessfull : Var = %0d ",obj.Var); else $display("Randomization failed"); end endprogram RESULT: Randomization sucsessfull : Var Randomization sucsessfull : Var Randomization sucsessfull : Var Randomization sucsessfull : Var Randomization sucsessfull : Var Randomization sucsessfull : Var Randomization sucsessfull : Var = 77 = 86 = 76 = 89 = 86 = 76 = 96

Overriding Datamembers Only virtual methods truly override methods in base classes. All other methods and properties do not override but provide name hiding.

EXAMPLE class base; int N = 3; function int get_N(); return N; endfunction endclass class ext extends base; int N = 4; function int get_N(); return N; endfunction function int get_N1(); return super.N; endfunction endclass

5of6

7/4/20128:44PM

WWW.TESTBENCH.INSystemverilogOOPS

http://www.testbench.in/CL_05_INHERITANCE.html

program main; initial begin ext e = new; base b = e; // Note same object! $display(b.get_N()); // "3" $display(e.get_N()); // "4" $display(e.get_N1()); // "3" - super.N end endprogram RESULT 3 4 3

<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer

6of6

7/4/20128:44PM

Vous aimerez peut-être aussi