How Does Inheritance Work for Attributes

How does inheritance work for Attributes?

When Inherited = true (which is the default) it means that the attribute you are creating can be inherited by sub-classes of the class decorated by the attribute.

So - if you create MyUberAttribute with [AttributeUsage (Inherited = true)]

[AttributeUsage (Inherited = True)]
MyUberAttribute : Attribute
{
string _SpecialName;
public string SpecialName
{
get { return _SpecialName; }
set { _SpecialName = value; }
}
}

Then use the Attribute by decorating a super-class...

[MyUberAttribute(SpecialName = "Bob")]
class MySuperClass
{
public void DoInterestingStuf () { ... }
}

If we create an sub-class of MySuperClass it will have this attribute...

class MySubClass : MySuperClass
{
...
}

Then instantiate an instance of MySubClass...

MySubClass MySubClassInstance = new MySubClass();

Then test to see if it has the attribute...

MySubClassInstance <--- now has the MyUberAttribute with "Bob" as the SpecialName value.

How does Attribute Inheritance in C++ Work?

C++ doesn't work like Python (which isn't surprising, they are two very different languages after all), and member variables defined in an inherited class really defines a new variable that is unrelated to the variables of the parent class.

One possible solution is to create a second (possibly protected) Base constructor which takes the name as an argument, and then the Derived class can use it to initialize the member:

struct Base {
Base() = default; // A defaulted default constructor

std::string name = "base";
void announce() {
std::cout << name << std::endl;
}

protected:
explicit Base(std::string name)
: name{ std::move(name) } // Initialize the member
{
}
};

struct Derived : public Base {
Derived()
: Base("movie") // Initialize the base class using the special constructor
{
}
};

The default Base default constructor is needed, because if you declare another constructor the compiler won't automatically generate a default constructor for you.

how does a subclass inherits a parent's attributes in python

When you call the super().__init__() within a subclass, you're just executing the parents init method and everything in that method.

The child class will inherit the attributes and all the methods without calling the super().__init__() function. However, if you defined an init function within the child class, then it will overwrite the copied parents class init method within the child class. You have to remember that when a child class inherits a parent class, it essentially makes a child class that is a duplicate of the parent class, which can be added to and/or altered without affecting the parent class. You can pick and choose which those things you want the child class to inherit from the parent by not overwriting them, and you can choose which of those you don't want to inherit by overwriting them.

Example 1

class parentclass:
b = 31

def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)

class childclass(parentclass):
pass

child1 = childclass()

child1.add(10,20)
print(child1.b)
print(child1.a)

Output:

30
31
21

Example 2:


class parentclass:
b = 31

def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)

class childclass(parentclass):
def __init__(self):
self.name="child"

child1 = childclass()

child1.add(10,20)
print(child1.b)
print(child1.a)

Output:

Traceback (most recent call last):
File "C:/Users/chees/PycharmProjects/untitled/idk.py", line 20, in <module>
print(child1.a)
AttributeError: 'childclass' object has no attribute 'a'
30
31

Edit:

Which is why the super().init() method is required within the child's init method, in order to copy the parents init method within the childs init method again, i.e:


class parentclass:
b = 31

def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)

class childclass(parentclass):

def __init__(self):
super().__init__()
self.name="child"

child1 = childclass()

child1.add(10,20)
print(child1.b)
print(child1.a)

is the same as

class parentclass:
b = 31

def __init__(self):
self.a = 21
def add(self, a, b):
c = a+b
print(c)

class childclass(parentclass):

def __init__(self):
self.a = 21
self.name="child"

child1 = childclass()

child1.add(10,20)
print(child1.b)
print(child1.a)

Are Method Attributes Inherited in C#?

It depends on how the attribute itself is declared - see AttributeUsageAttribute.Inherited property.

The inheritance of attributes using __init__

When writing the __init__ function for a class in python, you should always call the __init__ function of its superclass. We can use this to pass the relevant attributes directly to the superclass, so your code would look like this:

class Person(object):
def __init__(self, name, phone):
self.name = name
self.phone = phone
class Teenager(Person):
def __init__(self, name, phone, website):
Person.__init__(self, name, phone)
self.website=website

As others have pointed out, you could replace the line

Person.__init__(self, name, phone)

with

super(Teenager, self).__init__(name, phone)

and the code will do the same thing. This is because in python instance.method(args) is just shorthand for Class.method(instance, args). If you want use super you need to make sure that you specify object as the base class for Person as I have done in my code.

The python documentation has more information about how to use the super keyword. The important thing in this case is that it tells python to look for the method __init__ in a superclass of self that is not Teenager

How to Inherit Attributes in Python

Try changing

def getNum(self):
return super().num

to

def getNum(self):
return self.num

and see if that helps :)



Related Topics



Leave a reply



Submit