inheritance
Subclass – Arguments From Superclass
There’s no magic happening! __init__ methods work just like all others. You need to explicitly take all the arguments you need in the subclass initialiser, and pass them through to the superclass. class Superclass(object): def __init__(self, arg1, arg2, arg3): #Initialise some variables #Call some methods class Subclass(Superclass): def __init__(self, subclass_arg1, *args, **kwargs): super(Subclass, self).__init__(*args, **kwargs) … Read more
Overriding getter in Swift
This works for me: public class MyBaseClass { private var _name: String = “Hi” public internal(set) var name: String { get { return self._name } set { self._name = newValue } } } public class MyDerivedClass:MyBaseClass { override public var name: String { get { return “Derived – \(super.name)” } set { super._name = newValue … Read more
Accessing the name of a private-inherited class from a subclass
This is a great bug! In the context of C, “A” means “the injected-class-name A that I get from my base”. You could think of it as C::B::A. Of course, that base is inaccessible due to private inheritance: class A {}; class B : private A { public: B() {} B(const A&) {} }; class … Read more
scope of private constructor in Nested Class
if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (ยง7.6) that encloses the declaration of the member or constructor. http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1
C#: interface inheritance getters/setters
Method hiding in an interface isn’t nearly as grungy; I’d go with something like: interface IBasicProps { int Priority { get; } string Name {get;} //… whatever } interface IBasicPropsWriteable:IBasicProps { new int Priority { get; set; } new string Name { get; set; } //… whatever } class Foo : IBasicPropsWriteable { public int … Read more
Is synchronized inherited in Java?
No, you will always have to write synchronized. If you call the synchronized method of the super class this will of course be a synchronized call. synchronized is not part of the method signature. See http://gee.cs.oswego.edu/dl/cpj/mechanics.html for detailed description from Doug Lea, Java threading boss (or so).
Multiple Inheritance Ambiguity with Interface
The diamond problem only applies to implementation inheritance (extends in all versions of Java prior to Java 8). It doesn’t apply to API inheritance (implements in all versions of Java prior to Java 8). Since interface methods with matching type signatures are compatible, there is no diamond problem if you inherit the same method signature … Read more
Is there a way to guarantee an interface extends a class in Java?
Java interfaces cannot extend classes, which makes sense since classes contain implementation details that cannot be specified within an interface.. The proper way to deal with this problem is to separate interface from implementation completely by turning Vehicle into an interface as well. The Car e.t.c. can extend the Vehicle interface to force the programmer … Read more
In C# 4.0, is it possible to derive a class from a generic type parameter?
Generic types in C# are not C++ templates; remember, a generic type must work for all possible type arguments. A template need only work for the constructions you actually make. This question is a duplicate; see my answer to Why cannot C# generics derive from one of the generic type parameters like they can in … Read more