Verification Guild
A Community of Verification Professionals

 Create an AccountHome | Calendar | Downloads | FAQ | Links | Site Admin | Your Account  

Login
Nickname

Password

Security Code: Security Code
Type Security Code
BACKWARD

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Modules
· Home
· Downloads
· FAQ
· Feedback
· Recommend Us
· Web Links
· Your Account

Advertising

Who's Online
There are currently, 61 guest(s) and 0 member(s) that are online.

You are Anonymous user. You can register for free by clicking here

  
Verification Guild: Forums

 Forum FAQForum FAQ   SearchSearch   UsergroupsUsergroups   ProfileProfile  ProfileDigest    Log inLog in 

Class Vs Abstract Class??
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Verification Guild Forum Index -> Main
View previous topic :: View next topic  
Author Message
desperado
Senior
Senior


Joined: Jan 16, 2009
Posts: 41

PostPosted: Tue Apr 06, 2010 7:16 am    Post subject: Class Vs Abstract Class?? Reply with quote

Hi All..

Whats the advantage of Abstract Class over the normal Class and Vice-Versa. Kindly brief me out!!

Thanks,
Desperado Twisted Evil
Back to top
View user's profile
AmreSultan
Senior
Senior


Joined: Jan 13, 2004
Posts: 77
Location: Ottawa, Canada

PostPosted: Tue Apr 06, 2010 8:19 am    Post subject: Reply with quote

Think of an abstract class as a template or like a Java interface if you're familiar.

The idea is to define the user interface via the method definitions but not provide the implementation. This is why it's 'abstract' and hence cannot be instantiated.

Now any classes that extend the abstract class can (and in fact must ) implement all the abstract methods (pure virtual methods in SV speak) in order to not be abstract themselves. Only when a class has no pure virtual methods can it be instantiated. I.e., all methods must have an implementation.

So this way you can force anyone that wants to extend your class to implement a read, write, or whatever method that was defined in the abstract class.

Amre

Amre
_________________
--Amre Sultan--
Back to top
View user's profile Visit poster's website
pavanshanbhag
Senior
Senior


Joined: Mar 25, 2009
Posts: 380
Location: Bangalore, India

PostPosted: Thu Apr 08, 2010 3:06 am    Post subject: Reply with quote

Hi Amresultan,

Quote:

The idea is to define the user interface via the method definitions but not provide the implementation. This is why it's 'abstract' and hence cannot be instantiated.


It can be instantiated but cannot call the constructor of the abstract class in the derived class. Although its meaningless to instantiate abstract class inside a derived class, since the derived class will extend the base abstact class.

For ex :
Code:

virtual class abstarct_base_class;
 -----
endclass

class derived_class extends  abstarct_base_class;
abstarct_base_class a_b_c; //Handle can be created, but its meaningless since we are       
                                               //extending the abstarct class
a_b_c = new (); // Constructor called - this is invalid
endclass;

_________________
-Pavan K Shanbhag

“The difference between genius and stupidity, genius knows his limits.” - Albert Einstein
Back to top
View user's profile Send e-mail Visit poster's website
AmreSultan
Senior
Senior


Joined: Jan 13, 2004
Posts: 77
Location: Ottawa, Canada

PostPosted: Thu Apr 08, 2010 7:31 am    Post subject: Reply with quote

Quote:
Code:
abstarct_base_class a_b_c; //Handle can be created, but its meaningless since we are       
                                               //extending the abstarct class


This is not instantiating the class. This is only declaring a variable or handle. The 'instantiation' is not done until an object is created, and hence memory allocated, through the calling of the constructor.

I believe I am using accepted OOP terminology correctly.

Regards,
_________________
--Amre Sultan--
Back to top
View user's profile Visit poster's website
pavanshanbhag
Senior
Senior


Joined: Mar 25, 2009
Posts: 380
Location: Bangalore, India

PostPosted: Thu Apr 22, 2010 12:20 am    Post subject: Reply with quote

HI Amresultan,

Quote:

The 'instantiation' is not done until an object is created, and hence memory allocated, through the calling of the constructor.


Yes you are right on it. Thanks for your valuable information.
_________________
-Pavan K Shanbhag

“The difference between genius and stupidity, genius knows his limits.” - Albert Einstein
Back to top
View user's profile Send e-mail Visit poster's website
BugCatcher
Senior
Senior


Joined: Apr 23, 2010
Posts: 43

PostPosted: Fri Apr 23, 2010 4:21 am    Post subject: Reply with quote

As commented by experts:

a) Abstract(Virtual) classes serve like a template.
Eg: I would like all my transaction classes to follow a *unique* style.

b) They can have virtual methods. Number of arguments can be fixed in the template(base class virtual method).
All the extensions *must* have the same number and type of arguments
(consistent code is always complete and will have less bugs)

c) polymorphism : The main use of virtual members. Base class can hold the derived class handle and you can call the methods.

special and interesting case:

D) Call to virtual method from constructor has a different functionality than compared to non-virtual methods.
suppose consider the following code:

Code:

class A
  virtual task x;
  ...
  endtask
  function new();
   x();
  enddunction
endclass

class B extends A;
  virtual task x;
  ...
  endtask
endclass

initial begin
  B obj = new()
end


The flow of execution will be
a) control will go to new() of class B
b) super.new() will pass the control to new of class A
c) The new of class A is calling x()
d) The point to note is "The B:Mad() will be executed first. not the A:Mad()". Since x is virtual.
if x is non-virtual A:Mad() will be executed.

experts am I correct?
Back to top
View user's profile Send e-mail
BugCatcher
Senior
Senior


Joined: Apr 23, 2010
Posts: 43

PostPosted: Fri Apr 23, 2010 5:47 am    Post subject: Reply with quote

Man... I meant to type scope resolution but not smiles
corrected the above code:

The flow of execution will be
a) control will go to new() of class B
b) super.new() will pass the control to new of class A
c) The new of class A is calling x()
d) The point to note is "The B :: x() will be executed first. not the A :: x()". Since x is virtual.
if x is non-virtual a :: x() will be executed.
_________________

-Bug Catcher
==============================
Back to top
View user's profile Send e-mail
AmreSultan
Senior
Senior


Joined: Jan 13, 2004
Posts: 77
Location: Ottawa, Canada

PostPosted: Fri Apr 23, 2010 8:03 am    Post subject: Reply with quote

Nothing wrong with the smilies BugCatcher!!

Just to clarify your example. There's nothing unique about the virtual method behaviour from within the constructor. The behaviour is the same no matter where you call the virtual method from. To illustrate the real power of polymorphism I would do the following.

Code:

initial begin

B b = new(); //derived class
A a = new(); //base class

a = b;

a.x(); //This calls the implementation of x() defined in b not a.
end



If x was not virtual the A:: x() would be called but because it is virtual B:: x() gets called.

So with non-virtual methods, what you see is what you get. You get the method implementation from the obj of the same type as the handle definition. Nothing special there.

With virtual methods, the method call is based on the runtime object that the handle is pointing to.

So in the above example imagine all the 'A' related code is in one verification component that knows nothing about the derived class B. Then you replace the 'a' object in that component with a B object and voila! You get your piece of 'A' Verification IP to behave like it was 'B' without ever touching the original code.
_________________
--Amre Sultan--
Back to top
View user's profile Visit poster's website
BugCatcher
Senior
Senior


Joined: Apr 23, 2010
Posts: 43

PostPosted: Fri Apr 23, 2010 11:15 am    Post subject: Reply with quote

Dear AmreSultan,

I have two programs here

Program 1: when the function 'x' is virtual:
Code:

program test();
class A;
  virtual function void x();
    $display(" CLASS A: METHOD X");
  endfunction
  function new();
    x();
  endfunction
endclass

class B extends A;
  virtual function void x();
    $display(" CLASS B: METHOD X");
  endfunction
  function new();
    super.new();
  endfunction
endclass

initial begin
  B obj = new();
end
endprogram


output of simulation is: CLASS B: METHOD X

Program 2: same as above except 'x' is non-virtual (no other change)

Code:


program test();
class A;
  function void x();
    $display(" CLASS A: METHOD X");
  endfunction
  function new();
    x();
  endfunction
endclass

class B extends A;
  function void x();
    $display(" CLASS B: METHOD X");
  endfunction
  function new();
    super.new();
  endfunction
endclass

initial begin
  B obj = new();
end
endprogram


output of simulation is: CLASS A: METHOD X

There is a difference right? do you agree?
try it and let me know...
_________________

-Bug Catcher
==============================
Back to top
View user's profile Send e-mail
AmreSultan
Senior
Senior


Joined: Jan 13, 2004
Posts: 77
Location: Ottawa, Canada

PostPosted: Fri Apr 23, 2010 1:28 pm    Post subject: Reply with quote

Hi BugCatcher,

So here's what I see is happening.

In Program1 the method x() is virtual.

When you call B = new() you get a call to super.new() which in turn calls the virtual method x() of 'this' object. At runtime the object it's referring to is of type B. The only object in your example is of type B because that's the only new call in your code. Hence there is no way the runtime type can be anything but B. I hope we agree so far. If so then because the methods are virtual the implementation of x() that gets called is going to be of the runtime type which we said was B so the output should be

CLASS B: METHOD X

which it is.

In Program2 the methods are not virtual so the print message you get should be of the 'compile time' type.

In this case the method call in A::new() is calling this.x() which in that context is of type A. At compile time we don't know that the x() call in the constructor is going to be called from a B obj.I hope we agree here as well. So the output we expect is that of A:: x()

CLASS A: METHOD X

which it is.

I guess the point I was trying to make (and obviously I did a poor job of it) is that the fact that you called the virtual method x() from within the constructor is inconsequential to the results. If you take the following example you should get similar results to above.

Program 3
Code:
class A;
  virtual function void x();
    $display(" CLASS A: METHOD X");
  endfunction
  function new(); 
     //nothing interesting here.
  endfunction
  function void y();
     x();
  endfunction
endclass

class B extends A;
  virtual function void x();
    $display(" CLASS B: METHOD X");
  endfunction
  function new();
    super.new();
  endfunction
  function void();
    super.y();
  endfunction
endclass

initial begin
  B obj = new();
  obj.y();
end


If the x() functions are virtual you get the same output as Program1 if the x() functions are not-virtual you get the same output as your Program2.

BugCatcher wrote:
Quote:

D) Call to virtual method from constructor has a different functionality than compared to non-virtual methods.


I guess my only disagreement was the "from constructor" part of your statement.

So your understanding of the polymorphic behaviour of virtual methods in SV is correct, but it's not governed by the fact that the method is called from the constructor in particular.

I hope this is a little clearer.

Thanks,
_________________
--Amre Sultan--
Back to top
View user's profile Visit poster's website
BugCatcher
Senior
Senior


Joined: Apr 23, 2010
Posts: 43

PostPosted: Fri Apr 23, 2010 1:42 pm    Post subject: Reply with quote

Yup. Thanks !!
_________________

-Bug Catcher
==============================
Back to top
View user's profile Send e-mail
BugCatcher
Senior
Senior


Joined: Apr 23, 2010
Posts: 43

PostPosted: Fri Apr 23, 2010 1:49 pm    Post subject: Reply with quote

Hello AmreSultan,

Yes. I agree with you. The caller need not be 'constructor'.
Following is the program and its output:

Code:

program test();

class A;
  virtual function void x();
    $display(" CLASS A: METHOD X");
  endfunction
  function void y();
    x();
  endfunction
endclass

class B extends A;
  virtual function void x();
    $display(" CLASS B: METHOD X");
  endfunction
  function void y();
    super.y();
  endfunction
endclass

initial begin
  B obj = new();
  obj.y();
end
endprogram


output: same as the call from constructor : CLASS B: METHOD X

Cheers,
_________________

-Bug Catcher
==============================
Back to top
View user's profile Send e-mail
AmreSultan
Senior
Senior


Joined: Jan 13, 2004
Posts: 77
Location: Ottawa, Canada

PostPosted: Fri Apr 23, 2010 1:50 pm    Post subject: Reply with quote

No problem.
_________________
--Amre Sultan--
Back to top
View user's profile Visit poster's website
desperado
Senior
Senior


Joined: Jan 16, 2009
Posts: 41

PostPosted: Sun Apr 25, 2010 11:51 pm    Post subject: Reply with quote

Hi All,

Truely thanks to all of your valueable comments & examples...

Thanks,
Desperado --> A Salute to all the Professionals & there Professionalism Razz
Back to top
View user's profile
nimesh
Newbie
Newbie


Joined: Jan 03, 2012
Posts: 2

PostPosted: Wed Jan 04, 2012 2:42 am    Post subject: Reply with quote

Dear Amer Sultan and BugCatcher,

I tried given example and observed following things.

When method x() called from constructor, it calls its own x() method even though it is defined as VIRTUAL and when it is called through y() method, it calls derived class x() method.

Below is a code and its simulation results.
Code:


program test();
class A;

  virtual function void x();
    $display(" CLASS A: METHOD X");
  endfunction

  function new();
    x();
  endfunction

  function void y();
    x();
  endfunction : y

endclass

class B extends A;

  virtual function void x();
    $display(" CLASS B: METHOD X");
  endfunction

  function new();
    super.new();
  endfunction

  function void y();
    super.y();
  endfunction : y

endclass

initial begin
  B obj = new(); // THIS PRINTS => CLASS A: METHOD X
  obj.y(); // THIS PRINTS => CLASS B: METHOD X
end
endprogram


======================================================
== Simulation Result ==
======================================================
QuestaSim-64 qverilog 6.5c Compiler 2009.08 Aug 27 2009
/tools/mentor/questa6.5c/questasim/bin/../linux_x86_64/qverilog virtual_task.sv
-- Compiling program test

Top level modules:
        test
+ /tools/mentor/questa6.5c/questasim/linux_x86_64/vsim -lib work test -c -do run -all; quit -f -appendlog -l qverilog.log -vopt
Reading /tools/mentor/questa6.5c/questasim/tcl/vsim/pref.tcl

# 6.5c

# vsim -appendlog -do {run -all; quit -f} -l qverilog.log -lib work -c -vopt test
# //  QuestaSim-64 6.5c Aug 27 2009 Linux 2.6.18-194.el5
# //
# //  Copyright 1991-2009 Mentor Graphics Corporation
# //              All Rights Reserved.
# //
# //  THIS WORK CONTAINS TRADE SECRET AND
# //  PROPRIETARY INFORMATION WHICH IS THE PROPERTY
# //  OF MENTOR GRAPHICS CORPORATION OR ITS LICENSORS
# //  AND IS SUBJECT TO LICENSE TERMS.
# //
# Loading sv_std.std
# Loading work.test(fast)
# run -all
#  CLASS A: METHOD X
#  CLASS B: METHOD X
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    Verification Guild Forum Index -> Main All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Verification Guild © 2006 Janick Bergeron
Web site engine's code is Copyright © 2003 by PHP-Nuke. All Rights Reserved. PHP-Nuke is Free Software released under the GNU/GPL license.
Page Generation: 0.610 Seconds