A
A
Albert2020-12-17 22:01:12
Programming languages
Albert, 2020-12-17 22:01:12

What languages ​​have a built-in ability and syntax to remove certain methods and/or properties of inherited classes when the class is created?

What languages ​​have this functionality?

class A:
    var a1
    var a2

class B extends A:
    remove a1


removeremoves a property a2from the class B, class instances Bare already created without a2

That is, when creating a new class, you can specify in it which properties / methods of inherited classes to remove. It shouldn’t work
in other places of the code . As far as I know, this can be implemented, for example, in Python using , but I’m interested in examples of languages ​​\u200b\u200bwith built-in syntactic support for such a feature (that is, where, when creating a class, you can specify, for example, using keyword: "let's remove these elements from this class). remove

type()

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Adamos, 2020-12-17
@Adamos

If a gloomy old woman knocks on your window tonight, it is Barbara Liskov who has a conversation with you.

S
shurshur, 2020-12-18
@shurshur

The whole point of inheritance is that an instance of class B can also be used as an instance of class A (but not vice versa). If some attribute is removed from B, what will happen to inheritance? If you just try to somehow hide it, then this will not help - after all, you can turn an instance of class B into an instance of class A by casting types and get back your access to the desired field.
And in general, here, for example, there is a library that can work with objects of class A. The library was compiled by someone, its source code is not available. We define a child class B and then use the library functions on objects of this class. Everything is fine, but we can no longer prohibit the library from working with this field.
PS: At one time, reading the description of the internal structure of classes in Turborland Pascal helped me understand OOP much better. Classes are arranged (in Pascal they are called objects, but this is not so important) something like this:
1. Link to the virtual method table (VMT)
2. Field 1.
3. Field 2.
...
VMT is structured like this:
1. Link to the method 1
2. Reference to the method 2
... The
inherited class differs from the original one in that it has a longer list of fields (but all the fields of the original are available and located at the same offsets), and the addresses of the overridden methods are different, plus itself the VMT table can be longer due to new methods added (but the same old methods are at the same offsets).
Due to this, even previously compiled code, which knows nothing about inherited classes, works with them as with their own.

V
Vasily Bannikov, 2020-12-18
@vabka

There is no such thing and cannot be, because it breaks the whole concept of inheritance.
Most likely, you want to do something wrong, since you have such a desire.
UPD: Looked at the comments. If you really want to take someone else's module and hide something from it, then you don't need to use inheritance.

X
xmoonlight, 2020-12-18
@xmoonlight

See the use of interfaces.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question