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.
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 derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
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.
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;
initial begin subclass s;
s = new();
s.printf();
end endprogram
RESULT
THIS IS PARENT CLASS
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.
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
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.
Programmers can override the existing constraint and replaces with new constraint as shown in below example.
EXAMPLE: class Base;
randinteger 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 = 77 Randomization sucsessfull : Var = 86 Randomization sucsessfull : Var = 76 Randomization sucsessfull : Var = 89 Randomization sucsessfull : Var = 86 Randomization sucsessfull : Var = 76 Randomization sucsessfull : Var = 96
Overriding Datamembers
Only virtual methods truly override methods in base classes. All other methods and properties do not override but provide name hiding.
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