Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass abstract class as parameter for instance of type?

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.

like image 489
HavelTheGreat Avatar asked Jan 13 '16 13:01

HavelTheGreat


People also ask

Can abstract class be used as a parameter type?

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.

Can I pass parameter in abstract method?

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.

Can you have an instance of an abstract class?

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.

Can abstract class create instance of its object?

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.


1 Answers

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.

like image 142
Yacoub Massad Avatar answered Sep 30 '22 17:09

Yacoub Massad