inheritance

Which methods can a subclass inherit in Java?

谁说我不能喝 提交于 2020-01-01 03:18:29
问题 Sorry I am a newbie to Java. I am trying to get my head around inheritance and subclass/superclass relationships in Java. If classA is a subclass of classB, will classA's protocol feature all methods that belong to classA or only those declared public and package? Can classA's protocol feature private methods inherited from its superclass ClassB? 回答1: Firstly, the word "inherited" isn't quite the right term. You mean "visible". public and protected are always visible private is not visible

Inheritance and the “this” keyword

。_饼干妹妹 提交于 2020-01-01 02:17:50
问题 Suppose that we have next situation: Parent class A: class A{ public A(){} public doSomething(){ System.out.println(this.getClass()); } } with a child class B: class B extends A{ public B(){} public void doSomething(){ super.doSomething(); System.out.println(this.getClass()); } } and Main class: class Main{ public static void main(String[] args){ A ab=new B(); ab.doSomething(); } } When I execute this code result is B B Why does this , referenced in superclass A, returns B as a class when the

Why Do I need to redeclare type constraint in generic subclass

戏子无情 提交于 2020-01-01 01:51:13
问题 Recently I tried to create a generic subclass by implementing a generic interface. public interface IModule<T> where T : DataBean { ..... } public class Module<T> : IModule<T> where T : DataBean { .... } It seems I can't rely on any of T's restrictions as were defined in the base interface, and I need to re-declare them myself. MSDN just provided: When using the subclass generic type parameters, you must repeat any constraints stipulated at the base class level at the subclass level. For

A polymorphic collection of Curiously Recurring Template Pattern (CRTP) in C++?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 00:52:07
问题 I've got a class Base from which I have two classes, DerivedA and DerivedB as defined below. template <typename Derived> class Base{ public: double interface(){ static_cast<Derived*>(this)->implementation(); } }; class DerivedA : public Base<DerivedA>{ public: double implementation(){ return 2.0;} }; class DerivedB : public Base<DerivedB>{ public: double implementation(){ return 1.0;} }; In short, I'm trying to do the following to maintain a collection of objects, some of which are DerivedA

deciphering vtable dumps

余生颓废 提交于 2019-12-31 22:23:58
问题 I am "playing" with virtual inheritance in C++, and I want to know how a class object is laid out. I have those three classes: class A { private: int a; public: A() {this->a = 47;} virtual void setInt(int x) {this->a = x;} virtual int getInt() {return this->a;} ~A() {this->a = 0;} }; class B { private: int b; public: B() {b = 48;} virtual void setInt(int x) {this->b = x;} virtual int getInt() {return this->b;} ~B() {b = 0;} }; class C : public A, public B { private: int c; public: C() {c = 49

Python mock: mocking base class for inheritance

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 22:15:44
问题 I am testing a class that inherits from another one very complex, with DB connection methods and a mess of dependences. I would like to mock its base class so that I can nicely play with the method defined in the subclass, but in the moment I inherit from a mocked class, the object itself turns a mock and loses all its methods. How can I mock a superclass? More or less the situation can be summed up in this: import mock ClassMock = mock.MagicMock() class RealClass(ClassMock): def lol(self):

Python mock: mocking base class for inheritance

吃可爱长大的小学妹 提交于 2019-12-31 22:14:09
问题 I am testing a class that inherits from another one very complex, with DB connection methods and a mess of dependences. I would like to mock its base class so that I can nicely play with the method defined in the subclass, but in the moment I inherit from a mocked class, the object itself turns a mock and loses all its methods. How can I mock a superclass? More or less the situation can be summed up in this: import mock ClassMock = mock.MagicMock() class RealClass(ClassMock): def lol(self):

Understanding (simple?) C++ Inheritance

半世苍凉 提交于 2019-12-31 19:28:14
问题 I'm struggling a bit to understand why this code snippet does not compile. #include <cstdio> class A { public: virtual int potential()=0; virtual int potential(int arg, int arg2)=0; }; class B : public A { public: int potential() { return 1; } virtual int potential(int arg, int arg2) { return 2; } }; class C : public B { public: int potential(int arg, int arg2) { return 3; } }; int main(int argc, char** argv) { C c; int value = c.potential(); printf("Got %i\n", value); return 0; } I have two

Generic method with type constraints or base class parameter

折月煮酒 提交于 2019-12-31 17:59:39
问题 If I write a method accepting a parameter which derives from a BaseClass (or an interface), as far as I know there are two ways to achieve that: void MyMethod<T>(T obj) where T : BaseClass { ... } and void MyMethod(BaseClass obj) { ... } What advantages / disadvantages has using the one over the other? 回答1: In this example there isn't a big difference between the two, you can access the same members inside the method and you can call it with the same derived classes. There is a runtime

Generic method with type constraints or base class parameter

有些话、适合烂在心里 提交于 2019-12-31 17:58:12
问题 If I write a method accepting a parameter which derives from a BaseClass (or an interface), as far as I know there are two ways to achieve that: void MyMethod<T>(T obj) where T : BaseClass { ... } and void MyMethod(BaseClass obj) { ... } What advantages / disadvantages has using the one over the other? 回答1: In this example there isn't a big difference between the two, you can access the same members inside the method and you can call it with the same derived classes. There is a runtime