Posts

Showing posts from June, 2017

Callbacks With Interfaces Or Delegates

I'll start like this by directly jumping into a scenario. You have this one class, which has many methods called by different other classes. For the moment let's not think about those many methods and who calls those. Focusing on the the matter in hand the requirement is such, that you have to call a method in some other class, which will consume some time for execution. So that method runs that execution in a background thread inside, and only he knows when it finishes. In this scenario you have to give them something to call, may be some reference or some callback method, after the execution of the background thread is done. What I'm going to discuss now is the ways to do it. Delegates First I'll take you on the C#.NET road. In C# there is a very simple and straight forward way to do this, thanks to " Delegates ", which are reference points to the methods. In simple words, in a delegate you can put a method in there,

Multiple Inheritance with Interfaces

First things first. What is multiple inheritance means ? When one class can inherit characteristics and features from multiple classes, it's called as multiple inheritance. As an example, class FuelVehicle{ public void fillFuel(){ } } class ElectricVehicle{ public void charge(){ } } class HybridCar extends ElectricVehicle,FuelVehicle{ } HybridCar class inherits from both FuelVehicle and ElectricVehicle classes. Then HybridCar class get the ability of using the features of both the mother classes. In simple words, an object of the HybridCar class can call both the methods, fillFuel() and charge(). Up to now, all is well. But, there can be a problem in a scenario like the following. class Vehicle{ public void start(){ } } class FuelVehicle extends Vehicle{ public void start(){ //Overridden } public void fillFuel(){ } } class ElectricVehicle extends Vehicle{ public void start(){ //