Inheritance:
//--------------------------------------------------------------------------------------------
//Inheritance:
//
//Inheritance is the mechanism which allows child class to inherit properties of parent
//class.
//--------------------------------------------------------------------------------------------
class A; // creating a class
int a=5; //class property
function void disp(); // class method
$display("1.Value of a = %0d",a);
endfunction:disp
endclass:A
//-------------------------------------------------------
//'extends' is the keyword which helps child class B
//to inherit the properties of base class A.
//
//-------------------------------------------------------
class B extends A; // inheritance
int a = 6; //class B properties
function void display(); //child class method
$display("2.Value of a = %0d",a);
endfunction:display
endclass:B
module inh_sam();
B b1; // creating handle for class B
initial begin
b1 = new; //creating an object
b1.a = 10; // accessing the class property
b1.disp(); //accessing the class method of parent class
b1.display(); //accessing the class method of child class
end
endmodule:inh_sam
Output:

Polymorphism:
class parent;
virtual function void display();
$display("This is parent class");
endfunction:display
endclass:parent
class child1 extends parent;
function void display();
$display("This is child class1");
endfunction:display
endclass:child1
class child2 extends parent; //child1 -same output
function void display();
$display("This is child class2");
endfunction:display
endclass:child2
module poly_ex();
parent p1[1:0];
child1 c;
child2 c2;
initial begin
c = new();
c2 = new();
p1[0] = c;
p1[1]=c2;
p1[0].display();
p1[1].display();
end
endmodule:poly_ex
Output:

Encapsulation:
local:
class parent;
int a=10;
local int b = 20;
local int c = 30;
function void display();
$display("parent class: a= %0d, b= %0d, c= %0d", a,b,c);
endfunction
endclass:parent
class child extends parent;
function void display();
$display("child class: a= %0d", a);
// $display("child class: b= %0d, c= %0d", b,c); //cannot access local variable
endfunction
endclass:child
module prot();
parent p;
child c;
initial begin
p = new();
c = new();
p.display();
// p.c = 40; //Cannot access local/protected member "p.d"
c.display();
end
endmodule:prot
Output:

Protected:
class parent;
int a=10;
protected int b = 20;
protected int c = 30;
function void display();
$display("parent class: a= %0d, b= %0d, c= %0d", a,b,c);
endfunction
endclass:parent
class child extends parent;
function void display();
$display("child class: a= %0d, b= %0d, c= %0d", a,b,c);
endfunction
endclass:child
module prot();
parent p;
child c;
initial begin
p = new();
c = new();
p.display();
// p.c = 40; //Cannot access local/protected member "p.d"
c.display();
end
endmodule:prot
Output:

Abstraction:
virtual class A;
int a,b,c;
pure virtual function void disp();
pure virtual task sum();
endclass:A
class B extends A;
function void disp(); // not really necessary to add vitual here
a =10;
$display("1.Value of a = %0d, b = %0d, c = %0d",a,b,c);
endfunction:disp
task sum();
c = a+b;
$display("2.Value of a = %0d, b = %0d, c = %0d",a,b,c);
endtask:sum
endclass:B
module pure_vir_fun_ex();
B b1;
initial begin
b1 = new;
b1.disp();
b1.b = 35;
b1.sum;
end
endmodule:pure_vir_fun_ex
Output:
