Are Interfaces just Multiple Inheritance?

Hola!

I'm a very experienced C++ programmer who has just started working with Unity at my new job, so I'm getting a lot of C# in. I've read a lot about interfaces but I'm missing a hard and fast explanation of why interfaces are considered such an important part of the language. The philosophy seems to be "Use interfaces for nearly everything."

There's this idea that objects and classes should be "protected" somehow behind the interfaces that they implement. I don't really see why this is superior to simply inheriting and using abstract base classes. Of course there's no multiple inheritance in C# so if you want to compose a class with more than one base behavior you must use interfaces and proxy objects, but is it a superior solution or merely a workaround?

In what way is an interface concretely better than inheritance?

πŸ‘︎ 66
πŸ’¬︎
πŸ‘€︎ u/Ikbensterdam
πŸ“…︎ Jul 02 2021
🚨︎ report
712 years of generation gold digging: so you should marry old people with the highest net worth and since they die soon you can collect multiple inheritances in one lifetime, donate their bodies to science and leave all your assets to 1 child reddit.com/gallery/nj2t2v
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/LOA_Nova
πŸ“…︎ May 23 2021
🚨︎ report
A question about super() and multiple inheritance...

If I run the following code I get an error.

class A:
    """
    This is the object which the other two objects
    will inherit from.
    """
    
    def __init__(self, a): print(a)

class B(A):
    """
    This is one of the parent objects.
    """

    def __init__(self, a, b): 
        super().__init__(a)
        print(b)

class C(A):
    """
    And the other one...
    """

    def __init__(self, a, c): 
        super().__init__(a)
        print(c)

class D(B, C):
    """
    And here's the problem:
    """

    def __init__(self, a, b, c, d):
        B.__init__(self, a, b)
        C.__init__(self, a, c)

        print(d)

D('a', 'b', 'c', 'd')

The problem with this code is that I have an object which inherits from two different objects that both use the super() method in their constructors. From the object D, I'm calling the constructors of the objects B and C. The problem is that I'm calling them using the parent classes' identifiers and passing the child object as the self argument. When the class B calls super().__init__, the interpreter understands that it's being called from the object D. The interpreter does its best and calls the constructor for C, but the constructor of B is calling the constructor of C via super() only with the parameter a, so a positional argument gets missing and I get this error:

Traceback (most recent call last):
  File ".../test.py", line 39, in <module>
    D('a', 'b', 'c', 'd')
  File ".../test.py", line 33, in __init__
    B.__init__(self, a, b)
  File ".../test.py", line 15, in __init__
    super().__init__(a)
TypeError: __init__() missing 1 required positional argument: 'c'

Does anyone know if I can choose which constructor to call using the super() function? Or if anyone has another better idea, I'd be thankful (because my code is quite a mess...).

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/sergiolbrallg
πŸ“…︎ May 16 2021
🚨︎ report
What happens to heirlooms when you die with multiple kids and split inheritance?

How does it decide which kid gets the heirlooms?

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/shmueliko
πŸ“…︎ May 13 2021
🚨︎ report
Inheritance with multiple constructors.

Hello!

I'm writing two classes, one called User and one called Worker, both classes have 3 constructors, one takes one argument for each member to initialize, one takes a string containing a json and the other contains a json object type from the library I use.

Basically, I have something that looks like this:

// classes.h
class User
{
    User(int id, std::string &username);
    User(std::string &jsonString);
    User(Json::Value &jsonObject);
};

class Worker : public User
{
    Worker(int id, std::string &username, std::string &firstName);
    Worker(std::string &jsonString);
    Worker(Json::Value &jsonObject);
};

I then proceed to define the constructors for User

// user.cpp
User::User(int id, std::string &username)
:
m_id {id},
m_username {username}
{}

User::User(std::string &jsonString)
{
    // Parse String
    // Read Values
}

User::User(Json::Value &jsonObject)
{
    // Read values
}

This works flawlessly as expected, the problems comes when I start dealing with Worker constructors. Since I'm inheriting a class that has a constructor, I need to satisfy said constructor, by doing something like the following

// worker.cpp
Worker::Worker(int id, std::string &username, std::string &firstName)
:
User(id, username),
m_firstName {firstName}

The issue however is that now when I want to define the other constructors Xcode yells at me because I need to satisfy specifically the first constructor that I declared... however I don't need to so, this constructor works in a completely different manner.

I managed to avoid the issue by creating a default constructor and making it private, but I'm not sure it's the best of ideas and solutions... how would you guys do it?

Basically, I added the following to the User class

protected:
User();

Unrelated question Another question that I have, as you can see I have two very similar constructors for both classes, one that takes a string, parses it, and then reads the values, and one that just reads the values from an already parsed object.

Basically I have a lot of duplicated code there, how do you think I should handle this? Create a private method to read the values and then call it from both constructors? Or is there a better way?

Thank's in advance for your help!

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/LynxesExe
πŸ“…︎ May 13 2021
🚨︎ report
repr functions with inheritance - why multiple prints?

I have this sample code

class Person:
    def __init__(self, fn, ln):
        self.fn = fn
        self.ln = ln
    
    def __repr__(self):
        print('Person name is ' + self.fn + ' ' + self.ln)
        
class Employee(Person):
    gid = 0
    def __init__(self, fn, ln, id=None):
        Employee.gid += 1
        self.id = Employee.gid
        super().__init__(fn, ln)
        
    def __repr__(self):
        print('Employee ' + self.fn + ' ' + self.ln + ' has id ' + str(self.id))
        
a = Employee('Kai', 'Kiu')
b = Employee('Koo', 'Kry')
c = Person('Hah', 'Pop')

upon running this prints outs

Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Koo Kry has id 2
Employee Kai Kiu has id 1
Employee Koo Kry has id 2
Employee Kai Kiu has id 1
Employee Kai Kiu has id 1
Employee Koo Kry has id 2
Person name is Hah Pop

My question : why is this (multiple prints) happening?
Shouldn't __repr__ be called only once?

Here is a li

... keep reading on reddit ➑

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/aceMe007
πŸ“…︎ May 02 2021
🚨︎ report
My inheritance is so large that I have to meet with multiple lawyers and shareholders, when all I want to do is holiday in New Zealand.
πŸ‘︎ 634
πŸ’¬︎
πŸ‘€︎ u/HelloIamacabbage
πŸ“…︎ Nov 27 2020
🚨︎ report
your opinion on using multiple inheritance for pure abstract classes in c++

I think it is pretty well known now that virtual inheritance is to be avoided when possible in the embedded world because of the need for extra memory for a vtable and extra steps to look up that vtable when calling a virtual function. Multiple inheritance is seen as even worse as it exhibits the same problems but this time with more lookup tables and perhaps, from a design perspective, needlessly added complexity.

I am tempted to use interface approach in the architectural design of a project I will work on for a ARM m4. However this will require multiple inheritance of pure abstract classes (I am unsure if leaving them pure virtual gives a performance benefit?). I am curious to hear your opinion on using this approach in embedded development from a performance/design tradeoff perspective. Have you used this approach in any of your projects?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/AEHadi
πŸ“…︎ Mar 05 2021
🚨︎ report
Ok so after a ton of research (two Google searches) I've found the best country to get a shit ton of money and drumroll...... it's Norway. They have really high wages, moderate tax and no inheritance tax which makes it perfect for multiple generations.
πŸ‘︎ 112
πŸ’¬︎
πŸ‘€︎ u/ibtidalord
πŸ“…︎ Nov 22 2020
🚨︎ report
vtable implementation with multiple inheritance

I've been searching around for specifics on how vtables are implemented in OO languages with multiple inheritance (regardless of whether it's only multiple interfaces like in Java or multiple base classes like in C++). Material I've found is actually quite vague / short on details so I was hoping maybe someone here has some direct experience with this.

In my language Tuplex I'm striving for fast dynamic dispatch for virtual methods - a single virtual method pointer lookup in the vtable, without executing an actual search algorithm. (It would be trivial to implement a search for a method ID in a list - but that would be significant overhead for the general case.) It seems this isn't actually possible in the general case since interfaces may be inherited in any order and via arbitrary inheritance chain of interfaces, which means there can be no static (known at compile time) position in a vtable to look up a given method in.

Perhaps C++ and other languages are doing something clever here to minimize overhead in virtual method dispatch. Go should also be interesting, it doesn't even declare interfaces in the implementing types. Anybody know how others have addressed this?

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/leswahn
πŸ“…︎ Jan 18 2021
🚨︎ report
Why multiple inheritance is not allow in GD Scripts ?

Curious drive me insane.

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/octor_stranger
πŸ“…︎ Jan 03 2021
🚨︎ report
Inheritance Tax and Multiple Kids

My dad passed leaving over 325k in his estate.

He didn’t write a will.

I understand the threshold is 325k, and then 40% above this, but is this per individual, or is it before its split up? He has 5 kids, so it would be under 325k each before tax.

Thanks!

πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/deweythesecond
πŸ“…︎ Jan 11 2021
🚨︎ report
Anime Only - I have a question regarding the "inheritance" of multiple titans and Ymir

TL;DR at the end

Sooo basically since Eren has both the Attack Titan and the Founding Titan he has "merged" two of the 9 Titans in one body. Lets say that one of the other Nine Titans ex Armored Titan eats Eren. Armored then has his, the Attack and the Founding Titan.

If this continued to happen until the last of the 9 titans has all 9 in one body (with the Curse just starting out). That person would basically be invincible. I wonder what they would look like?πŸ€”

Anyways they would merge into Ymirs starting titan right? After the timer runs out and they die of the Curse , would the 9 titans within split off again and each titan go to different children that are born at that time? Or will they all go to one child? Another similar, but smaller question qould be if Eren dies and doesn't get eaten by another titan would the Founding Titan and the Attack Titan also split off into each child or come in a package deal?

I would assume that they would merge into the starting Titan that Ymir was. But I wouldn't know how they would split off again. My guess would be that they would be divided through family or something like the original but I don't know how.

ATTENTION REGARDING REVEALING SPOILERS!:

I am an avid consumer of AoT and have read some spoilers on the fandom pages and have been exposed to some spoilers in the r/titanfolk subreddit. I want an answer that has relevant manga spoilers, like for Erens founding and attack titan getting a third. Nothing about Marley and Paradis island and stuff like that. I know I am being very picky, but I just want to enjoy the anime. I will probably read the manga after both are finshed. Odds are I won't read this until after the show is finished but I just want to know how it works and before I forget the question. I hope you can give me a spoiler free answer. If you would be so kind to mark spoiler somewhere on your comment that would be great so that others would get stuff spoiled for them.

TL;DR Basically what would happen if all of the 9 Titans were in one body? Would they Merge? And if that Titan Shifter dies, would each Titan go to individual children or go to one child in a package? MENTION IT IN YOUR COMMENT IF YOUR ANSWER CONTAINS MAJOR/HEAVY STORY RELATED SPOILERS

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/AlkhaleelS1
πŸ“…︎ Dec 31 2020
🚨︎ report
Hand rolled open hierarchy RTTI single header library with support for auto ID generation, multiple inheritance and dynamic casting.

For an embedded C++ uC project I have recently been working on a hand-rolled RTTI to replace C++'s inefficient build in RTTI. It is inspired by the examples in the guidelines defined for RTTI by the LLVM project but with the extension that it supports:

  • Automatic ID generation
  • Multiple inheritance (no diamond Β―\(ツ)/Β―)
  • Dynamic casting

It works for our application, which has a decently sized inheritance hierarchy, but I am looking to further extend it usage and gain some feedback from the field.

I hope you find it useful, enjoy!

https://github.com/royvandam/rtti

πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/red4black
πŸ“…︎ Dec 03 2020
🚨︎ report
Python Multiple Inheritance - Tutorial 21 youtu.be/chVdfrrIs_w
πŸ‘︎ 7
πŸ’¬︎
πŸ‘€︎ u/hacknomus
πŸ“…︎ Jan 27 2021
🚨︎ report
TIL C# doesn't support multiple inheritance.
πŸ‘︎ 369
πŸ’¬︎
πŸ‘€︎ u/abhi_000
πŸ“…︎ May 14 2020
🚨︎ report
Vtable layout differences between Itanium and MSVC ABI under multiple inheritance

If you have a derived class inheriting from 2 base classes, and you override a virtual function in the second base class, the Itanium ABI requires the overriden virtual function to appear under the vtable of the first base class.

See here for an example. Class CC overrides function v, and you can see in Clang's vtable a CC::v under AA's vtable. Whereas the MSCV ABI behaves as I would expect, only the BB vtable is changed, the AA vtable is unchanged.

Any idea why the Itanium ABI behaves this way? And advantage to this?

πŸ‘︎ 56
πŸ’¬︎
πŸ‘€︎ u/whichton
πŸ“…︎ Sep 02 2020
🚨︎ report
Multiple inheritance with unrelated classes

I'm currently working on a project where I originally had 2 classes, where one inherits from the other.

It looks a little like this:

class A(object):

def __init(input):

pass

class B(A):

def __init__(input):

super().__init__(input)

But now, I have an additional class I want to inherit. It looks a little like this:

class A(object):

def __init(input1):

pass

class C(object):

def __init(input2):

pass

class B(A,C):

def __init__(input1,input2):

#help!

I am now stuck because I don't know how to inherit both of the classes. They are not related in any way and don't call on each other. I've tried doing

super().__init__([input1,input2])

to see if that would work, but it did not. I've tried looking online to see how other people are doing it but I can't seem to find an answer that is related to my problem. Any advice? Thanks.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/laudeac
πŸ“…︎ Dec 15 2020
🚨︎ report
V-tables in Clang: how C++ multiple, virtual inheritance is implemented shaharmike.com/cpp/vtable…
πŸ‘︎ 30
πŸ’¬︎
πŸ‘€︎ u/crassest-Crassius
πŸ“…︎ Oct 25 2020
🚨︎ report
Mixins and multiple class inheritance for JavaScript and TypeScript youtube.com/watch?v=JNmqx…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/basaratali
πŸ“…︎ Dec 21 2020
🚨︎ report
[CK3 Question] Inheritance with multiple kingdoms

Hello!

I'm a bit confused on how succession works in regards to multiple kingdoms.

I am playing as Sweden, and my character is in control of "The Kingdom of Sweden" and "The Kingdom of Norway". It is all part of sweden, but the different kingdoms have seperate elections.

The succession laws are Confederate partition.

When my character dies, will the "country" of Sweden be split up into 2 different countries, or is the king of Norway going to become my vassal?

What are some ways I can make sure that the country isn't split up? I am in the year 945, so primogeniture isn't an option.

Hopefully this question makes sense, I'm pretty confused in regards to succession even after reading articles on it and watching a couple of videos.

Thank you for reading!

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/Tweekylol
πŸ“…︎ Sep 25 2020
🚨︎ report
Is lack of multiple inheritance a flaw?

Does C#'s lack of multiple inheritance limit the capabilities of the language? Do you wish multiple inheritance was supported?

πŸ‘︎ 14
πŸ’¬︎
πŸ‘€︎ u/TheTruth_1998
πŸ“…︎ Apr 22 2020
🚨︎ report
Multiple inheritance.
πŸ‘︎ 28
πŸ’¬︎
πŸ‘€︎ u/solvorn
πŸ“…︎ Jun 08 2020
🚨︎ report
Multiple Inheritance in Python

Here it is multiple inheritances, My below code is quitting with the TypeError

TypeError: object.__init__() takes exactly one argument (the instance to initialize)

The possible cause for the above error: - Here, Class 'A' is calling the constructor of class 'D' (because of multiple inheritances in class 'E') and class 'B' is calling the constructor of the class 'object'. the super-class 'object' always takes exactly one argument.

Can anyone suggest the solution for this above problem?

I am looking for the right approach to be followed consistently in all the classes without considering specific (single/multiple) inheritances, but at the end, it works well when a developer start using them in the single/multiple inheritance hierarchy. That is; I want a generic approach to be followed in each class which works smoothly with single/multiple inheritances

Do we have any ready decorator which suffice the need, if not, can we write one?

class A(object):
    def __init__(self, *args, **kwargs):
        print("This is A constructor")
        super(A, self).__init__(*args, **kwargs)

class B(object):
    def __init__(self, *args, **kwargs):
        print("This is B constructor")
        super(B, self).__init__(*args, **kwargs)

class C(A):
    def __init__(self, arg, *args, **kwargs):
        print("This is C constructor")
        self.arg = arg
        super(C, self).__init__(arg, *args, **kwargs)

class D(B):
    def __init__(self, arg, *args, **kwargs):
        print("This is D constructor")
        self.arg = arg
        super(D, self).__init__(arg, *args, **kwargs)

class E(C,D):
    def __init__(self, arg, *args, **kwargs):
        print("This is E constructor")
        super(E, self).__init__(arg, *args, **kwargs)

E(10)
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/gmaliwal
πŸ“…︎ Mar 27 2020
🚨︎ report
An Exhaustively-Analyzed IDB for ComRAT v4 - ComRAT v4 is a large, sophisticated backdoor that uses many C++ features: polymorphism, templates, multiple/virtual inheritance, many parts of the STL, etc. This is the result of six weeks of static RE, with no symbols/RTTI. msreverseengineering.com/…
πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/digicat
πŸ“…︎ Sep 02 2020
🚨︎ report
Multiple inheritance
πŸ‘︎ 45
πŸ’¬︎
πŸ‘€︎ u/solvorn
πŸ“…︎ Jun 08 2020
🚨︎ report
C++ and Multiple Inheritance.
πŸ‘︎ 185
πŸ’¬︎
πŸ‘€︎ u/h1v3r_XD
πŸ“…︎ Jul 03 2019
🚨︎ report
Multiple inheritance
πŸ‘︎ 19
πŸ’¬︎
πŸ‘€︎ u/solvorn
πŸ“…︎ Jun 08 2020
🚨︎ report
Object construction order in case of virtual and/or multiple inheritances

https://medium.com/pranayaggarwal25/object-construction-in-case-of-virtual-multiple-inheritance-dcf8909f228b?source=friends_link&sk=5edca79de54234b12f0ea54c2f344aaa

Hi All,

I have compiled an article on how does virtual and/or multiple inheritance affects object construction order of a class?

With some reading from Herb's blog and isocpp, I have put together this, which covers -

  1. What are the rules for object construction order?
  2. Understanding the object construction order rules in detail
  3. Applying the rules to correctly identify the order?
  4. What is the exact order of destructors in a multiple and/or virtual inheritance situation?

:) Please help me in improving anything which is incorrect or missing in this.

πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/pranayaggarwal25
πŸ“…︎ Jul 06 2020
🚨︎ report
Help regarding multiple inheritance concept

Why do I have to pass the self keyword in line 15 (i.e return A.truth(self))?

class A:
    def truth(self):
        return 'All numbers are even'
    
class B(A):
    pass

class C(A):
    def truth(self):
        return 'Some numbers are even'

class D(B,C):
    def truth(self,num):
        if num%2 == 0:
            return A.truth(self)
        else:
            return super().truth()

>d = D()
>d.truth(6)
>'All numbers are even'
>d.truth(5)
>'Some numbers are even'
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/mythrowaway0852
πŸ“…︎ Jun 04 2020
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.