Can you give me an almost overly simplistic understanding of abstract class vs inheritance use and help me so I can truly understand the concept and how to implement? I have
Generally speaking:
An interfaces describe the methods an object will respond to. It is a contract the object commits to satisfy.
Abstract classes describes basic functionality and let specialized functionality to a subclass.
So, basically you use an interface when you want that objects different in nature, respond to the same specific method.
And you use an abstract class when you need to have specialized versions of some class.
Let's say you want to create a system where any kind of object may be identified by an unique id, and you don't care the class they belong to.
You may have:
Animals
Transport
Computer gadgets.
Whatever.
Since they are unrelated topics, you may choose to implement and interface, let's say:
public interface IIdentifiable
{
public long GetUniqueId();
}
And all the classes that want to satisfy this contract will implement that interface.
public class IPod: IIdentifiable
{
public long GetUniqueId()
{
return this.serialNum + this.otherId;
}
}
public class Cat: IIdentifiable
{
public long GetUniqueId()
{
return this.....
}
}
Both, and IPod and a Cat, have very different natures, but they both may respond to the "GetUniqueId()" method, that will be used in the catalog system.
Then it may be used like this:
...
IIdentifiable ipod = new IPod();
IIdentifiable gardfield = new Cat();
store( ipod );
store( gardfield );
....
public void store( IIdentifiable object )
{
long uniqueId = object.GetUniqueId();
// save it to db or whatever.
}
On the other hand, you may have an abstract class defining all common behavior the object may have, and let the subclass define specialized versions.
public abstract class Car
{
// Common attributes between different cars
private Tires[] tires; // 4 tires for most of them
private Wheel wheel; // 1 wheel for most of them.
// this may be different depending on the car implementation.
public abstract void move();
}
class ElectricCar: Car
{
public void move()
{
startElectricEngine();
connectBattery();
deploySolarShields();
trasnformEnertyToMovemetInWheels();
}
}
class SteamCar: Car
{
public void move()
{
fillWithWather();
boilWater();
waitForCorrectTemperature();
keepWaiting();
releasePreasure....
}
}
Here, two kinds of cars, implements the "move" method in different ways, still they share common things in the base class.
To make things more interesting, these two cars may implement also de IIdentifiable interface, but by doing so, they are just commiting to respond to the GetUniqueId method, and not by the nature of being cars. That's why the Car it self may not implement that interface.
Of course, if the identification may be based on the common attributes the cars may have, the GetIdentifiableId may be implemented by the base class and the subclasses will inherit that method.
// case 1 ... each subclass implements the interface
public class ElectricCar: Car, IIdentifiable
{
public void move()
{
.....
}
public long GetUniqueId()
{
....
}
}
public class SteamCar: Car, IIdentifiable
{
public void move()
{
.....
}
public long GetUniqueId()
{
....
}
}
Case 2, the base class implements the interface and the subclass benefit from it.
public abstract class Car: IIdentifiable
{
// common attributes here
...
...
...
public abstract void move();
public long GetUniqueId()
{
// compute the tires, wheel, and any other attribute
// and generate an unique id here.
}
}
public class ElectricCar: Car
{
public void move()
{
.....
}
}
public class SteamCar: Car
{
public void move()
{
.....
}
}
I hope this helps.