0

(Basic) Beings and matters. The first inheritance.

Background

Remember our last discussion about "Interface" and "Abstract Class"? Cannot decide between those two, yet? It's ok. Let's bring them all together. Because right now, we will create more of them, by inheriting them from the first ancestor of our universe, the "thing". protozoa.jpg

And then there are "beings" and "matters"

All "things" made after the "thing" must also have a name and ID. So, let's make some classifications. How about separating everything into two kinds? The living things and the inanimate objects. Living things are born, grow and die. On the other hand, inanimate objects are created and destroyed, by nature or by any kind of life forms.

"Wait".

"Isn't that the same scheme we're talking about?" "Born" and "created". "Die" and "destroyed". "Grow"? Hmm, ok, that's the difference. So, let's start with this : living things are aging. They also have health points and stamina. When their health points reach zero, they will die. Stamina affect their thoughness, to take any challenge coming to them. On the other hand, inanimate objects have similar attribute called "durability". How about that, huh? So far, so good? Ok, now let's just create them, and call them as "beings" and "matters".

For interface lovers, you could create them as two interfaces by deriving it from the "thing", like this :

Interface

public interface IBeing : IThing
{
    int Age { get; }
    int HealthPoints { get; }
    int Stamina { get; }
}

public interface IMatter : IThing
{
    int Durability { get; }
}

Meanwhile, for you fans of the class, you could create two abstract classes, to get the job done :

Class

public abstract class CBeing : CThing
{
    public abstract int Age { get; }
    public abstract int HealthPoints { get; }
    public abstract int Stamina { get; }
}

public abstract class CMatter : CThing
{
    public abstract int Durability { get; }
}

How lovely, isn't it, when these two approaches can co-exist together, side by side, just like this.

This is what we call "inheritance"

and it makes your life easier, doesn't it? You don't need to make sure that everything has its own name and ID, as long as those things are classified as a "thing". It just works, this way. Plain and simple.

"Hmm.. could you please tell me something I don't know?"

Ok, right. I guess you already know about this as well.

What we've learned so far?

###Inheritance is a method to create an object based on another object (a class or an interface) to easily maintain the same consistent behaviour. Still, derived objects can have their own specific behaviours, other than the ones inherited from the base object.

So, what's next?

"Ok just stop all of this crap, about creating abstractions or something unreal. Just create something real, will you?" Ok then. Now I think the time is right to talk about "implementations". Next article, please!


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí