Showing posts with label virtual. Show all posts
Showing posts with label virtual. Show all posts

Thursday, 7 January 2016

OOPS Interview Questions.

What is OOPS?

OOPS is abbreviated as Object Oriented Programming system
It is a programming language model organized around objects rather than "actions" and data rather than logic.
Programming approach that provided modularity means both data_members (attributes) and methods (function or task) can be used as templates for creating copies of such modules on demand.

 

What are the Basic Concepts of OOPs?

  1. Abstraction.
  2. Encapsulation.
  3. Inheritance.
  4. Polymorphism.

 

What is an object?

An object is a variable of user defined data type class.
Or,
Object is an instance of a class that is created dynamically.

 

What is Abstraction?

Abstraction refers to the process of representing only essential or relevant data to the users without showing unnecessary or background information.
Users just need to give appropriate arguments to the method while using it, internal complexity or background process is not shown to users. They get the result what they want.
Since class is a concept of data abstraction, it is known as abstract data type (ADT).

 

What is Encapsulation?

Prevents the data from unwanted access by binding of code and data in a single unit called object/class.
Or,
The wrapping up of data & methods into a single unit (called class) is known as encapsulation. Here class acts like a container.
These methods provide interface between program and object/class’s data. The insulation of data from direct access by the program is called data hiding. (Keep in mind that all data_member are declared as private)

 

What is Inheritance?

Promotes the re-usability of code and eliminates the use of redundant code.
Inheritance allows a Child class to inherit properties (member, method) from its parent class.
Only properties with access modifier public (local keyword in sv) and protected (protected keyword in sv) can be accessed in child class. (by default all properties are public)
In SystemVerilog this is achieved by using extends keyword.

 

What is multiple inheritance?

Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit properties from more than one class.
Systemverilog doesn’t support multiple inheritance. To overcome this problem it allows to implement multiple interface-class.

 

Is it possible for a class to inherit the constructor of its base class?

No, a class cannot inherit the constructor of its base class.

 

Can you call a base class method without creating instance?

Yes. But,
  • It’s possible if it’s a static method.
  • It’s possible if you derive the class from base class then take instance of derived class when call method by using derived class’s instance.
  • It’s possible from derived classes using keyword “super”.

 

What is polymorphism?

The ability to define a method in multiple forms is called Polymorphism.
In systemverilog there are two types of polymorphism:
  1. Compile time polymorphism (overloading) and
  2. Runtime polymorphism (overriding).
Overloading:
When a class has more than one method with same name but different parameters, then we call those methods are overloaded.
Overloaded methods will have same name but different number of arguments or different types of arguments or both.
Overloaded methods can be either static or non-static.

However System Verilog doesn't support Method Overloading (except constructor).

Overriding:
Language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super class or parent class.
Actual method which will be called is determined by the type of object at run-time.
Static methods cannot be overridden.
Two virtual methods must have the same signature.

 

What are the rules to be followed while overriding a method?

  • Name of the method must be same,
  • Return type of method must be same,
  • Parameter list of a method must be same,
  • Must not reduce the visibility.

 

What is method signature? What are the things it consists of?

Method signature is used by the compiler to differentiate the methods. Method signature consists of three things.
  • Method name
  • Number of arguments
  • Types of arguments

 

Virtual vs pure_virtual?

Virtual: If you forgot to override method in child class then at run-time, parent class’s method will be executed. If your parent class’s method is empty, then nothing will happen and even you will not get any warning/error for that. This will leads to unexpected behavior of code.
Pure virtual: If you forgot to override method in child class then at compile-time error will be shouted. So, you will come to know that you forgot to override that particular method in child class. This will stop unexpected behavior of code.
Pure virtual method is also called as abstract method.

 

What is abstract class?

An abstract class is a class that is declared as a virtual class.
Abstract classes are those which can be used for creation of handles. However their methods and constructors can be used by the child or extended class. The need for abstract classes is that you can generalize the super class from which child classes can share its methods.
A class is called abstract class if it is only used as a super-class for other classes.
Derived classes must define the properties of abstract class.
Abstract classes cannot be instantiated.
The subclass of an abstract class which can create an object is called as "concrete class".

 

Explain the concept of constructor?

Constructor is a special method of a class, which is called automatically when the instance of a class is created. It is created with the same name as the class and initializes all class members, whenever you access the class. The main features of a constructor are as follows:
  • Constructors do not have any return type.
  • Constructors can be overloaded.
  • It is not mandatory to declare a constructor; it is invoked automatically.

Monday, 1 June 2015

Virtual method in SystemVerilog

A method of a class may be identified with the keyword virtual.
Virtual methods are a basic polymorphic construct.
One way to view this is that there is only one implementation of a virtual method per class hierarchy, and it is always the one in the latest derived class.
Virtual methods provide prototypes for the methods that later override them.
Virtual method overrides in subclasses shall have matching argument types, identical argument names, identical qualifiers, and identical directions to the prototype.
The virtual qualifier is optional in the derived class method declarations. The return type of a virtual function shall be either:
matching type or a derived class type
of the return type of the virtual function in the superclass. It is not necessary to have matching default expressions, but the presence of a default shall match.

A virtual method may override a non-virtual method, but once a method has been identified as virtual, it shall remain virtual in any subclass that overrides it. In that case, the virtual keyword may be used in later declarations, but is not required.


class Base;
  int unsigned A = 1;
  int unsigned B = 2;

  function void printA();
    $display("Base: A=%0d", A);
  endfunction : printA

  virtual function void printB();
    $display("Base: B=%0d", B);
  endfunction : printB
endclass : Base

class Child extends Base;
  int unsigned A = 3;
  int unsigned B = 4;

  function void printA();
    $display("Child: A=%0d", A);
  endfunction : printA

  function void printB();
    $display("Child: B=%0d", B);
  endfunction : printB
endclass : Child

// Child2 is inherited (extends) from Child.
// printB is not defined as virtual in Child.
// Though Child2 override printB of Child because printB is defined as virtual in Base.

class Child2 extends Child;
  int unsigned A = 5;
  int unsigned B = 6;

  function void printA();
    $display("Child2: A=%0d", A);
  endfunction : printA

  function void printB();
    $display("Child2: B=%0d", B);
  endfunction : printB
endclass : Child2

module top();
  Base  B1;
  Child C1;
  Child2 C2;

  initial begin
    B1 = new();
    C1 = new();
    C2 = new();
   
    B1.printA;  // displays 'Base::A is 1'
    B1.printB;
  // displays 'Base::B is 2'
    C1.printA;  // displays 'Child::A is 3'
    C1.printB;  // displays 'Child::B is 4'
    C2.printA;  // displays 'Child2::A is 5'
    C2.printB;  // displays 'Child2::B is 6'

    B1 = C1;    // B1 has handle of Child
    B1.printA;  //
displays 'Base::A is 1'
    B1.printB;  // displays 'Child::B is 4' - latest derived method
    C1.printA;  // displays 'Child1::A is 3'
    C1.printB;  // displays 'Child1::B is 4'

    C1 = C2;     // C1 has handle of Child2
    C1.printA;  // displays 'Child1::A is 3'
    C1.printB;  // displays 'Child2::B is 6' - latest derived method
    C2.printA;  // displays 'Child2::A is 5'
    C2.printB;  // displays 'Child2::B is 6'

    B1 = C2;     // C1 has handle of Child2
    B1.printA;  // displays 'Base::A is 1'
    B1.printB;  // displays 'Child2::B is 6' - latest derived method
    C2.printA;  // displays 'Child2::A is 5'
    C2.printB;  // displays 'Child2::B is 6'
  end
endmodule : top


//Output:
// Base: A=1
// Base: B=2
// Child: A=3
// Child: B=4
// Child2: A=5
// Child2: B=6

// Base: A=1
// Child: B=4
// Child: A=3
// Child: B=4

// Child: A=3
// Child2: B=6
// Child2: A=5
// Child2: B=6

// Base: A=1
// Child2: B=6
// Child2: A=5
// Child2: B=6

Friday, 8 May 2015

Down Cast in SystemVerilog and virtual concept in SystemVerilog

// Command to run in questa
// 1. vlib work
// 2. vlog -sv top.sv
// 3. vsim top -c
// 4. run -all (in vsim prompt)

class base;

  int a = 5;
endclass

class extend extends base;
  int b = 1;
endclass

module top;
  initial begin
    base     m_base;
    extend  m_extend;
    
    m_extend = new();
    m_base    = new();
    $cast(m_extend, m_base);
    $display(m_extend.a);
  end
endmodule

//Error: (vsim-3971) $cast to type 'class work.top1_sv_unit::extend' from 'class work.top1_sv_unit::base' failed in file top.sv

Meaning of a successfully casting from super-class(parent class) to sub-class(child class) is that the super-class is actually a sub-class.

class BasePacket;
  int A = 1;
  int C = 2;

  function void printA;
    $display("BasePacket::A is %d", A);
  endfunction : printA

  virtual function void printC;
    $display("BasePacket::C is %d", C);
  endfunction : printC
endclass : BasePacket

class My_Packet extends BasePacket;
  int A = 3;
  int C = 4;

  function void printA;
    $display("My_Packet::A is %d", A);
  endfunction: printA

  virtual function void printC;
    $display("My_Packet::C is %d", C);
  endfunction : printC
endclass : My_Packet

module top;
  initial begin
My_Packet EXT = new;
BasePacket BASE;

BASE = EXT;
BASE.printA;
BASE.printC;
$cast(EXT, BASE);
EXT.printA;
EXT.printC;
end
endmodule : top

//Output:
// BasePacket::A is           1
// My_Packet::C is           4
// My_Packet::A is           3
// My_Packet::C is           4

Here super-class in this example (BASE) is actually the sub-class (EXT), so the $cast is passed.

Here for first call (using BASE handle) of printA and printC, super-class (BASE) is acutally sub-class (EXT) and function printC is declared as virtual in super-class (BASE), so it will execute printC function of sub-class (EXT).
But, function printA is not declared as virtual in super-class (BASE), so it will execute printA function of super-class (BASE), in-spite of it is actually having memory of EXT class, because handle is of super-class.

One usual scenario to use the $cast is to pass some sub-class data into other class, and the developer of the receptor class even don't know if the incoming class is what kind of sub-class, so usually he will in the receptor class use a super(parent)-class to get the incoming data and then use $cast to check/transfer the data into certain sub-class.