How can we return Candle, it is an interface with all the get function abstract, what should an object of the interface contain that we can add it to the list and return it.
See we are not creating object of type Candle we are making reference.
Here, Candle is interface and TiingoCandle is class which is implementing that interface.
If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.
But, using this you can access the methods of the interface only, if you try to access the methods of the class a compile time error is generated.
Refer this link to see example how we can do this in real world scenario.
We are just returning a list of reference variables which can hold objects of its implementation class(TiingoCandle).
Also try to figure out what happens if we return List of TiingoCandle directly instead of List of Candle and why it is inappropriate to do so.
The idea behind having the return type as Candle is to ensure that the method you are making is compatible with any class that implements the interface Candle. This is especially useful if we wish to add another API service, something that is being done in modules later.
When we have a return type Candle, you can return an object of any class that implements the interface Candle. In this case, it will be the class TiingoCandle. During the run time, the reference is resolved and the object’s type determines which class is used to refer to methods that are being called from the object.
To answer your question in abstract manner, you need to create an object of TiingoCandle class since it implements the Candle interface and return a list of that type. That should work.
If you have any issues implementing this, please let me know!
Thank you all for replying.highly appreciate your effort in writing such detailed post. I got the point that we are just adding reference variable of Candle interface to object of TiingoCandle class, but how this reference variables are used in function calculateAnnualizedReturn because here i am not getting how to get the buyPrice and sellPrice. It requires tiingo but the function only passess portfolioTrade and endDate as parameter. Should i use this list of reference variables to get the open and close price of a given stock??
Think of it this way.
You have the trade and end date. That’ll be used for the API. Then whatever the API returns, you’ll use it further, in your case which will be getting the buyPrice and sellPrice.
Do note that you’ll need to read the TODOs carefully to figure out what is the buyPrice and sellPrice for the given stock.
Let me know if you have any other doubts!
Hey @shubh, there are many ways to achieve your purpose. I will list out some as Hints!
Interface : Candlestick
Implementing class : Wick
1. ArrayList<Candlestick> candlesticks = new ArrayList<Candlestick>(wicks);//Creating a ArrayList<Candlestick> object by copying ArrayList<Wick> object *** 2. Candlestick candlestick = (Candlestick)wick; //Casting class to interface 3. Candlestick candlestick = new Wick(); //Giving reference of Interface type
Hoping this lighten the candles!
Closing this topic as your issue has been resolved by the community.