Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a derived class method from the base class?

I have read several similar questions about this but none seem to solve the problem I am facing. The typical answer is to cast as the derived class but I cannot since I do not know the derived class type.

Here is my example:

class WireLessDevice { // base class
    void websocket.parsemessage(); // inserts data into the wireless device object
}

class WiFi : WireLessDevice { // derived class
    GPSLoc Loc;
}

Wireless Device can also be derived to make Bluetooth, Wi-Max, Cellular, etc. devices and thus I do not know which type of wirelessdevice will be receiving the data.

When a GPS packet is received on the websocket in the base class I need to update the derived device's location.

I thought perhaps sending a message via a queue or creating an event handler and sending the location in the event arguments but those seem a little clunky when the data is staying within the class.

Is there something built into the language that would allow me to call my derived device from the base class without knowing the type?

like image 467
CramerTV Avatar asked Apr 18 '13 16:04

CramerTV


People also ask

Can we call derived class method from base class?

Even though the derived class can't call it in the base class, the base class can call it which effectively calls down to the (appropriate) derived class. And that's what the Template Method pattern is all about.

Can we call derived class method from base class in Java?

Yes, derived classes have immediate access to objects and methods in the Base Classes they derive from ... if ... the Base Class exposes those objects and methods by the use of modifiers, like 'public.

When a class is derived from a base class it gets access to?

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Constructors, destructors and copy constructors of the base class.


2 Answers

The correct way is to add a method DoSomeMagic() in the base class, with default implementation, or abstract. The derived class should than override it to do its magic.

Something like this maybe:

public class WireLessDevice
{ // base class
    protected virtual void ParseMessage()
    {
        // Do common stuff in here
    }
}

public class WiFi : WireLessDevice
{ // derived class
    override void ParseMessage()
    {
        base.ParseMessage();//Call this if you need some operations from base impl.
        DoGPSStuff();
    }
    private void DoGPSStuff()
    {
        //some gps stuff
    }
    GPSLoc Loc;
}
like image 159
omer schleifer Avatar answered Oct 08 '22 02:10

omer schleifer


Virtual methods would be a good solution. You create a method ParseMessage in the base class which handles all the stuff common to each derived type, then override for specific devices.

In order to still be able to handle other message types, you will need to call the base.ParseMessage as appropriate:

class WireLessDevice
{
    protected virtual bool ParseMessage(byte[] message)
    {
        // Inspect message and handle the known ones
        return true; // Only if message parsed, otherwise false
    }
}

class WiFi : WireLessDevice
{
    GPSLoc Loc;

    protected override bool ParseMessage(byte[] message)      
    {
        bool messageParsed = base.ParseMessage(message)
        if (!messageParsed)
        {
            // Handle device specific message with GPSLoc
            Loc = ...
        }
        else
        {
            return false;
        }
    }
}

If your base class is not handling any other messages, the code could be simpler (and using abstract). However, in that case I would consider moving the web socket code to the derived class.

like image 42
Thorarin Avatar answered Oct 08 '22 03:10

Thorarin