abstract

Making a vector of instances of different subclasses

跟風遠走 提交于 2019-11-28 00:16:38
Tried searching, nothing returns( i ithink). Is it possible to make a vector of an abstract class? For example, I have the super class Unit. And I Have the subclasses soldier, vehicle, and bomber. However I would want ever instance of any subclass in one vector, e.g. the vector UnitList can hold instances of both soldier and vehicle? Is this possible? I'm using C++ if it helps. Yes, but you'll need to use either pointers or smart pointers (I'd go with this). struct X { virtual ~X() {} //<--- as pointed out in the comments // a virtual destructor is required // for correct deletion virtual void

Java abstract class fields override

醉酒当歌 提交于 2019-11-27 23:44:53
I have an abstract class that should implement a public field, this field is an interface or another abstract classe. something like this: public abstract class GenericContainer { public GenericChild child; } public abstract class GenericChild { public int prop1=1; } public abstract class SpecialChild extend GenericChild { public int prop1=2; } Now i have another specialized class Container: public abstract class SpecialContainer extends GenericContainer { public SpecialChild child=new SpecialChild(); //PAY ATTENTION HERE! } Java allow me to compile this, and i IMAGINE that the field child in

C#, implement 'static abstract' like methods

社会主义新天地 提交于 2019-11-27 23:24:56
I recently ran into a problem where it seems I need a 'static abstract' method. I know why it is impossible, but how can I work around this limitation? For example I have an abstract class which has a description string. Since this string is common for all instances, it is marked as static, but I want to require that all classes derived from this class provide their own Description property so I marked it as abstract: abstract class AbstractBase { ... public static abstract string Description{get;} ... } It won't compile of course. I thought of using interfaces but interfaces may not contain

I am unable to add an element to a list? UnsupportedOperationException

早过忘川 提交于 2019-11-27 23:18:03
问题 This one list object is biting me in the butt.. Any time I try to add an element to it, it produces this: Caused by: java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) The line producing the error is insignificant, but here it is anyways: AdventureLobbies.players.add(args[0].toLowerCase()); Should I not be accessing it statically? Actual declaration of variable: AdventureLobbies.players = Arrays

How can I force a Constructor to be defined in all subclass of my abstract class

巧了我就是萌 提交于 2019-11-27 19:57:24
I have an abstract class A that define abstract methods. This means that, for a class to be instanciable, all the abstract method have to be implemented. I'd like all my subclasses to implement a constructor with 2 ints as parameters. Declaring a constructor defeats my purpose, as I want the constructor defined in subclasses and I don't know anything about the implementation. Moreover I cannot declare a constructor as being abstract; Is there a way to do this ? Example of what I want: Lets say that I am defining the API of a Matrix class. In my problem, Matrix cannot change their dimensions.

reference to abstract class

谁都会走 提交于 2019-11-27 19:54:59
What does it mean when there is a reference to an abstract class? I found it in code and I can't understant it. I thought that an abstract class can't be instantiated. How can you give it a reference? A reference to an abstract class is just like a pointer to an abstract class: it needs to reference an object of some non-abstract subclass of the abstract class. You can use a reference like that to call virtual methods on the referenced class using the . syntax, in a way similar to a pointer to an interface in Java. An abstract class is designed to be derived from. The Liskov substitution

Defining an abstract class without any abstract methods

家住魔仙堡 提交于 2019-11-27 19:50:08
问题 Can I define an abstract class without adding an abstract method? 回答1: Of course. Declaring a class abstract only means that you don't allow it to be instantiated on its own. Declaring a method abstract means that subclasses have to provide an implementation for that method. The two are separate concepts, though obviously you can't have an abstract method in a non-abstract class. You can even have abstract classes with final methods but never the other way around. 回答2: Yes you can do it. Why

Why not abstract fields?

依然范特西╮ 提交于 2019-11-27 19:12:37
Why can't Java classes have abstract fields like they can have abstract methods? For example: I have two classes that extend the same abstract base class. These two classes each have a method that is identical except for a String constant, which happens to be an error message, within them. If fields could be abstract, I could make this constant abstract and pull the method up into the base class. Instead, I have to create an abstract method, called getErrMsg() in this case, that returns the String, override this method in the two derived classes, and then I can pull up the method (which now

Abstract methods in Java

为君一笑 提交于 2019-11-27 18:09:44
I want to write an abstract method but the compiler persistently gives this error: abstract methods cannot have a body I have a method like this: public abstract boolean isChanged() { return smt else... } What is wrong here? Reverend Gonzo Abstract methods means there is no default implementation for it and an implementing class will provide the details. Essentially, you would have abstract class AbstractObject { public abstract void method(); } class ImplementingObject extends AbstractObject { public void method() { doSomething(); } } So, it's exactly as the error states: your abstract method

Not sure when to use an abstract property and when not

徘徊边缘 提交于 2019-11-27 17:42:22
I'm not really sure what looks better or when do I really use in abstract classes and properties, or when to use non abstract properties. I'll try to make a simple example. Let's say I have this: abstract class Human { public GenderType Gender { get; set; } public string Name { get; set; } public Date Born { get; set; } public bool IsNerd { get; set; } abstract public void Speak(); abstract public void Sleep(); abstract public void AnoyingPeopleOnStackOverflow(); //... so on } class Peter : Human { //Peter is special, he got a second name //But thats all, everything else is the same as like on