nus-cs2103-AY2021S2 / forum

10 stars 0 forks source link

When inheritance does not mean a sub-type relationship exists #324

Closed tlylt closed 3 years ago

tlylt commented 3 years ago

In the textbook, it is stated that

Inheritance does not necessarily mean a sub-type relationship exists. However, the two often go hand-in-hand. For simplicity, at this point let us assume inheritance implies a sub-type relationship.

I am curious in what situation will inheritance not constitute a sub-type relationship?

pyuxiang commented 3 years ago

No idea, but I have a gut feeling that inheritance => subtype is a language-dependent concept. In Java, this is obvious since the interface and class constructs have strong adherence to OOP design philosophy. Other languages may borrow the inheritance syntax for alternative features, e.g. delegation, mixins. Some examples in Python (which is not pure OOP):

Mixin for comparable interface

class Comparable:
    def __eq__(self, other):
        return not (self < oth) and not (oth < self)
    ...

def EventDate(Date, Comparable): # inheritance
    def __init__(self, date):
        super().__init__(date)
    def __lt__(self, other):
        return self.date < other.date

print(EventDate("2021-04-19") == EventDate("2021-04-19")) # True

Mixin for singleton pattern

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super()(*args, **kwargs)
        return cls._instances[cls]

class Factory(metaclass=Singleton): # another inheritance
    pass

print(Factory("chocolate")) # chocolate
print(Factory("shoe")) # chocolate

There's also this random paper I found via Google, so it's probably not an isolated concept: https://www.cs.utexas.edu/users/wcook/papers/InheritanceSubtyping90/CookPOPL90.pdf

kouyk commented 3 years ago

There's also this random paper I found via Google, so it's probably not an isolated concept: https://www.cs.utexas.edu/users/wcook/papers/InheritanceSubtyping90/CookPOPL90.pdf

Just to add more confusion, there's also this paper: https://www.cs.rice.edu/~javaplt/papers/Inheritance.pdf

But I think this is a good read, the article discusses inheritance from a more theoretical standpoint to make it clear why inheritance isn't necessarily implying subtyping.

tlylt commented 3 years ago

Thank you @pyuxiang and @kouyk for your insightful comment, will definitely look that look at the papers when I have time.


But I think this is a good read, the article discusses inheritance from a more theoretical standpoint to make it clear why inheritance isn't necessarily implying subtyping.

@kouyk I read it but I am not sure if the points are valid...It is kinda interesting how he discussed method overriding and the notion of this/self, but doesn't seem convincing to me...The first part just showcased how he didn't understand method overriding and did not use the @Override annotation to enforce that? Anyways I guess people are entitled to their opinions...

damithc commented 3 years ago

As you guys can see, this is a somewhat 'contentious' topic and the arguments are not easily accessible to everyone, unless one has a deep-enough knowledge of programming language theory. Certainly outside the scope of this module.

Anyhow, try to avoid using inheritance and sub typing as synonyms, to be safe.