C# Casting Generic Child Type to Parent

Let's say we have these types: class A {} class B : A {} class X<T> {} Why we can't do this? X<A> var = new X<B>(); Is there any workaround available? [Edit] I tried to use covariance but it failed because I want to access a...
more »

2017-06-18 14:06 (2) Answers

Class cross referencing?

I come from a C++ background and I am a bit lost without the whole pointer concept in python. Or at least it's unclear. For instance, I want to create a game of tick tack toe using OOP in Python. I have a couple classes like so: class Game(object): ...
more »

2017-06-18 01:06 (1) Answers

Java return only attributes of superclass

I am trying to create an API controller with java and spring boot. I want to return specific objects as JSON from specific API endpoints, nothing special so far. Now I have a child class A and a parent class B. B is abstract and A is not abstract, bu...
more »

2017-06-17 11:06 (0) Answers

Confused with generics and subtyping

So, let's say we have a simple interface public interface ICopyable<T> { void copyFrom(T original); } Which basically adds a copyFrom method with a parameterized type. And you have an interface extending it: public interface ISomeObjec...
more »

2017-06-16 18:06 (0) Answers

Constraint on method parameters to enforce type

I have a situation where I have a base class and several derived classes. I would like to have a method that can operate on an item and a list of the same class type. I was receiving a compiler error because the type of item and list within the Proce...
more »

2017-06-16 15:06 (0) Answers

C++: Class within another class as type?

could someone explain me this kind of "inheritance" which can be found in class Y: private? class X { private: char c_; public: X(char c) : c_(c){} }; class Y { private: X x_; // What is this ? public: Y(X x): x_(x){} }; int main() { X m...
more »

2017-06-15 18:06 (1) Answers

Why is the id same for these two objects?

In the below code, why is the id of parent and child the same? Should they not be different? class Parent: def __init__(self): print(id(self)) class Child(Parent): def __init__(self): print(id(self)) super().__init__...
more »

2017-05-26 15:05 (1) Answers

Typescript :: Abstract static

In one of my personal projects, I was trying to design a few object and list types. The objects and lists are supposed to be serializable (ie have a toJSON() and fromJSON() method). A sample object and list would have the following basic code: type ...
more »

2017-05-26 09:05 (1) Answers

class variable update/inherit in instance method

I'm trying to update a variable in classA and used the updated info in classB. Hence I have two questions: 1) How to update class variable in instance method? 2) How to use inherit variables from parent class? The main idea of this program is that u...
more »

2017-05-24 09:05 (1) Answers

Swift Generic type used in Protocol not work

I simplify my trouble into a small demo about using Generic Type and protocol.here is the code. protocol Food { } class Meat: Food { } class Cake: Food { } protocol EatProtocol { func eat<T: Food>(_: T) } class Person: EatProtocol {...
more »

2017-05-24 04:05 (2) Answers

class inheritance multiple choice

Using tkinter and i have two exactly the same classes (tk and toplevel) so i merged them but need to choose which window type i want to inherit. code class Window(tk.Tk, tk.Toplevel): """ Basic root window (derived from tk.Tk) """ def __ini...
more »

2017-05-23 16:05 (1) Answers

Kotlin Inheritance

I'm currently learning to split my messy code into DDD (note, learning) paradigm. The entities (IEntity, IUser) is domain layer is made up of interface, which then implemented at the Data layer (BaseEntity, User). Access to data is through the reposi...
more »

2017-05-17 03:05 (2) Answers

Make a class that cannot be instantiated?

Suppose that I have a class Bar. I want to use Bar uniquely as a base class, e.g.: class Foo : public Bar I want to disallow Bar from being able to be instantiated. In other words, I want Bar object; to be an error. Is this possible to do? ...
more »

2017-05-15 00:05 (1) Answers

Common method uses unknown object type java

I am writing some java code and I am unsure of what structure I should use. I have different classes that each contain a populate method which receives a line of text from a text file. The contents of the text file are specific to the class. I need ...
more »

2017-05-12 19:05 (1) Answers

How to call abstract class method

I have an interface called Hospital.java public interface Hospital { public void operate(); public void doScan(); public void doVaccination(); } I have an abstract class called StreetHospital.java public abstract class StreetHospital ...
more »

2017-05-10 07:05 (12) Answers