So I have a type called FunBond
, with a default constructor like this
public FunBond(int id,
string name,
Currency currency,
double notional,
DateTime maturityDate,
List<CashFlowWithDate> couponCashFlows,
DayCounter dayCounter)
: base(id, name, notional, InstrumentType.FixedRateBond, currency, maturityDate, dayCounter)
{
...
}
And DayCounter
is an abstract class
public abstract class DayCounter
{
public abstract string Name();
public abstract double YearFraction(DateTime d1, DateTime d2);
public abstract int DayCount(DateTime d1, DateTime d2);
}
Now my question is, what am I supposed to supply as a DayCounter
to generate an instance of a FunBond
? I can specify the id, name, currency, ... etc. etc. to pass as parameters to FunBond
, but what is DayCounter
supposed to be? I can't create an instance of it, and I can't supply it with anything I don't think... I thought I would need another class deriving from my abstract class to provide to FunBond
, so I think I misunderstand something fundamentally.
An abstract class shall not be used as a parameter type, as a function return type, or as the type of an explicit conversion. Pointers and references to an abstract class can be declared. Passing obj as a const reference is allowed. Save this answer.
Yes, we can provide parameters to abstract method but it is must to provide same type of parameters to the implemented methods we wrote in the derived classes.
No, you cannot create an instance of an abstract class because it does not have a complete implementation. The purpose of an abstract class is to function as a base for subclasses. It acts like a template, or an empty or partially empty structure, you should extend it and build on it before you can use it.
Abstract class, we have heard that abstract class are classes which can have abstract methods and it can't be instantiated. We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.
The constructor is declaring that it requires an parameter of type DayCounter
. Since DayCounter
is an abstract class then you need to pass an instance of a class that derives from DayCounter
.
That does not mean that you need to change the constructor parameter type, just pass an instance of a derived type.
The reason that someone defines a parameter of an abstract type is to allow polymorphism and loose coupling. You can have different derived classes of DayCounter
, each behaving differently (as long as they adhere to the contract of DayCounter
). And FunBond
can speak to instances of such classes without knowing how they internally work. As far as FunBond
is concerned, it is speaking to an object that adheres to the contract of DayCounter
, but doesn't care how it internally implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With