inheritance

Making the inner class of a generic class extend Throwable [duplicate]

谁说我不能喝 提交于 2020-08-19 11:31:31
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why doesn’t Java allow generic subclasses of Throwable? I'm trying to make a regular RuntimeException inside a generic class like this: public class SomeGenericClass<SomeType> { public class SomeInternalException extends RuntimeException { [...] } [...] } This piece of code gives me an error on the word RuntimeException saying The generic class SomeGenericClass<SomeType>.SomeInternalException may not subclass

Inheritance rules in java

本小妞迷上赌 提交于 2020-08-19 07:47:37
问题 I have a question about the basics of java. I have the s attribute in each class. The value of s gotten by the instance of the class is different when I use the accessor (getS()). Is there a rule for this case? The output of the main is : x.s = One x.getS() = Three The classes definition : package com; import com.Test1.A; import com.Test1.B; public class Test1 { public static class A { public static String s = "One"; public int x = 10; public String getS() { return this.s; } } public static

Inheritance rules in java

陌路散爱 提交于 2020-08-19 07:45:09
问题 I have a question about the basics of java. I have the s attribute in each class. The value of s gotten by the instance of the class is different when I use the accessor (getS()). Is there a rule for this case? The output of the main is : x.s = One x.getS() = Three The classes definition : package com; import com.Test1.A; import com.Test1.B; public class Test1 { public static class A { public static String s = "One"; public int x = 10; public String getS() { return this.s; } } public static

Is this-> mandatory to access Base<T> identifiers from derived classes?

蹲街弑〆低调 提交于 2020-08-18 08:29:19
问题 This code compiles with MSVC 2015, but doesn't compile with Clang 5.0.0 (trunk 304874): template <typename T> struct Base { T data; }; template <typename T> struct Derived : Base<T> { auto getData() const { return data; } }; Replacing data with this->data in Derived::getdata() makes Clang happy. Which compiler is correct according to the C++ standard? Must this-> be used in template code to access an identifier of a base class? 回答1: Clang is correct. $17.6.2/3 Dependent names [temp.dep] In

How can we invoke the parent's method, when a child has a method with the same name in JavaScript? Anything like type casting in Java here? [duplicate]

做~自己de王妃 提交于 2020-08-10 01:24:37
问题 This question already has answers here : How to call a parent method from child class in javascript? (8 answers) Closed 10 days ago . class Parent { constructor(x) { this.x = x; } present() { return `I have a ${this.x}`; } } class Child extends Parent { constructor(x, y) { super(x); this.y = y; } present() { return `${super.present()}, it is a ${this.y}`; } } child = new Child("Tinggu", "Winggu"); console.log(child.present()); // invokes the child version How would I invoke the parent's

How can we invoke the parent's method, when a child has a method with the same name in JavaScript? Anything like type casting in Java here? [duplicate]

流过昼夜 提交于 2020-08-10 01:23:07
问题 This question already has answers here : How to call a parent method from child class in javascript? (8 answers) Closed 10 days ago . class Parent { constructor(x) { this.x = x; } present() { return `I have a ${this.x}`; } } class Child extends Parent { constructor(x, y) { super(x); this.y = y; } present() { return `${super.present()}, it is a ${this.y}`; } } child = new Child("Tinggu", "Winggu"); console.log(child.present()); // invokes the child version How would I invoke the parent's

Can we use super() to test identity between class methods in the MRO?

主宰稳场 提交于 2020-08-09 09:13:06
问题 Consider the following example: class A: def m(): pass class B(A): pass And the following terminal output: >>> b = B() >>> b.m <bound method A.m of <__main__.B object at 0x000001EFFF24C748>> >>> super(b.__class__, b).m <bound method A.m of <__main__.B object at 0x000001EFFF24C748>> >>> b.m is super(b.__class__, b).m False >>> b.m == super(b.__class__, b).m True Why are they equal but not identical? Is a copy of the method made when it is inherited? Are there better ways to test whether a

Overriding a virtual function with a covariant return type in a template derived class

筅森魡賤 提交于 2020-08-08 07:03:14
问题 I would like to override a virtual function in a template-derived class. However, I would like to use the derived class as return type. Here is the corresponding code: class Abstract { public: virtual Abstract* allocate() const = 0; }; template <typename Derived> class Base : public Abstract { public: Derived* allocate() const override { return new Derived; } }; class Concrete : public Base<Concrete> { public: }; int main() { Concrete c; delete c.allocate(); } Unfortunately, my compiler does

Overriding a virtual function with a covariant return type in a template derived class

╄→尐↘猪︶ㄣ 提交于 2020-08-08 07:03:12
问题 I would like to override a virtual function in a template-derived class. However, I would like to use the derived class as return type. Here is the corresponding code: class Abstract { public: virtual Abstract* allocate() const = 0; }; template <typename Derived> class Base : public Abstract { public: Derived* allocate() const override { return new Derived; } }; class Concrete : public Base<Concrete> { public: }; int main() { Concrete c; delete c.allocate(); } Unfortunately, my compiler does

Convert List of Base Class to a List of Derived Class - possible?

半城伤御伤魂 提交于 2020-07-30 02:41:15
问题 I coworker of mine told me that it was possible to convert a List<BaseClass> to a List<DerivedClass> (they didn't show me a working example). I didn't think it was possible to convert a parent class into a child class (I do know however that it is possible to do it the other way around). I've seen several related questions with people saying it can't be done and people saying it can - none of the proposed solutions have worked for me at all. I always get a: System.InvalidCastException. Unable