Newbie on Inheritance, Base classes and derived classes

A

aaj

Hi all

I'm new to OOP and just getting to grips with inheritance.

Could anyone tell me if my understanding so far is correct.....?

If I have a base class, and then some derived classes with extra
functionality e.g.

Base Class : Watch Method SetTime
Derived Class : DigitalWatch Method ChangeBattery
Derived Class : AnalogWatch Method WindUp

is it acceptable to use the base class type to access the SetTime method in
all the watches and then in the same app to use the derived class type for
only digital watches

so in one place use


Watch CurrentWatch = (watch) AndysDigitalWatch; i.e. casting Digital watch
as a base type watch and only seeing the SetTime Method

and in another place use

DigitalWatch CurrentWatch = AndysDigitalWatch;

obviously it wouldn't be exactly like this, I'm thinking more when using
foreach on the base class object, bringing back all the watches derived from
the base class, but only the methods common to the base class and the
derived class.

So in summary, I guess what I'm asking is - is it acceptable to declare
types of Base class to do some functions common to all objects, and in the
same app, but in a different place, use the more specialised derived classes

thanks

Andy
 
M

Morten Wennevik

Hi Andy,

You seem to have grasped the inheritance. In fact any derived class can be referred to as the parent class (assuming the parent isn't abstract). No cast is needed.

Watch w = new DigitalWatch();

Only methods defined in Watch will be visible to w.
Furthermore, objects retain knowledge of what they really are so you can cast this Watch back to DigitalWatch if needed.

DigitalWatch d = (DigitalWatch)w;

Considering a list of digital and analog watches.

ArrayList list = new ArrayList();
list.Add(new DigitalWatch());
list.Add(new AnalogWatch());
list.Add(new DigitalWatch());
list.Add(new DigitalWatch());
list.Add(new AnalogWatch());

You can then do

foreach(Watch w in list)
{
w.SetTime();
}

If you need different ways to set time for analog and digital watches, you can mark the base method virtual and override them in the inherited classes. Watch.SetTime would then either call DigitalWatch.SetTime or AnalogWatch.SetTime even if you use a base class reference.

In an external class, for instance in a button click event:

Watch w = new Watch();
w.DoStuff();
w = new DigitalWatch();
w.DoStuff();
w = new AnalogWatch();
w.DoStuff();

class Watch
{
protected virtual void SetTime(DateTime time){MessageBox.Show("Watch");}
public void DoStuff(){SetTime(DateTime.Now);}
}

class DigitalWatch : Watch
{
protected override void SetTime(DateTime time){MessageBox.Show("Digital");}
void ChangeBattery(){}
}

class AnalogWatch : Watch
{
void SetTime(DateTime time){MessageBox.Show("Analog");}
void WindUp(){}
}

It will output Watch-Digital-Watch since AnalogWatch does not override SetTime, calling DoStuff will use the base DoStuff method, and this method will then use the base SetTime method.


In addition to calling common base class methods you can implement interfaces and use a common interface to call on interface methods.
 
A

aaj

Morten

many thanks for spending time on the well thought out reply and examples.

Its taken me some time grasping OOP , but the fog is starting to lift a
little bit. Its nice to know my understanding of inheritance isn't too far
removed from what it should be :cool:

thanks again

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top