abstract

Overriding abstract property using more specified return type (covariance)

戏子无情 提交于 2019-11-29 15:55:37
class Base {} abstract class A { abstract public List<Base> Items { get; set; } } class Derived : Base {} class B : A { private List<Derived> items; public override List<Derived> Items { get { return items; } set { items = value; } } } The compiler says that B.Items must be List of Base elements "to match overridden member" A.Items. How can i make that work? Eugene Podskal What you've tried to accomplish initially is impossible - .NET does not support co(contra)variance for method overload . The same goes for properties, because properties are just the pair of methods . But you can make your

Private constructor in abstract class

允我心安 提交于 2019-11-29 12:16:22
问题 In Java what is the purpose of using private constructor in an abstract class? In a review I got this question, and I am curious, for what situation we need to use the constructor in such way? I think it can be used in pair with another constructor in abstract class, but this is very trivial. Also it can be used for constructing static inner classes which will excend abstract class. Maybe there is more elegant usage? 回答1: If the private constructor is the only constructor of the class, then

What are the differences between Hashmap vs Hashtable in theory?

半腔热情 提交于 2019-11-29 12:07:30
Are there are differences between hashmap and hashtable in theory? I don't mean in the concrete definitions given in Java (or the implementation), but in theory. Isn't a hashtable a map that uses hashing ... hence a hashmap? woliveirajr According to Wikipedia , they are the same: In computing, a hash table (hash map) is a data structure used to implement an associative array (...) According to Wikibooks , it's the same: A hash table, or a hash map, is a data structure that associates keys with values. Some answer on StackOverflow also states: Hashtable is often useful (they are also called

What are the rules to handle homonym inherited methods?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 10:51:41
I'm trying to understand how Java handles cases of ambiguity that come out when a concrete class inherits (abstract or concrete) methods having the same name from different classes/interfaces. I've not been able to find a general rule, this is why I decided, once for all, to spend some time on this by using a practical approach. I considered 8 different cases, combining abstract methods non-abstract methods abstract classes interfaces resulting in this scheme: +-------------------------+ | INTERFACE | +----------+--------------| | abstract | non-abstract | | method | method | +-----------+----

Use of an abstract class without any abstract methods

橙三吉。 提交于 2019-11-29 09:21:32
An abstract class need not include any abstract methods. Is there any other reason to make a class abstract other than the fact that abstract classes can't be instantiated? The principal role of an abstract class is to provide an appropriate root class from which concrete, (i.e. non-abstract) subclasses can be derived. This is a powerful and versatile feature which promotes code re-use. Abstract classes encapsulate general features that are common to a range of data types - features which are too general to be meaningful in the abstract class, but which can be overridden in a subclass Any

Abstraction and abstract in java

末鹿安然 提交于 2019-11-29 07:03:24
I am a java developer with good understanding of Object orientation concepts( or maybe, I think like that ). And right now I am learning design patterns (From Head first design patterns). I have been reading about OOPS concept abstraction to understand it briefly, and reading more about it has made me more confusing than I earlier was. As I understand, abstraction refers to hiding the internal details of the program while exposing the interface to other programmers without worries of internal details. But, I don't understand How abstract classes fit into this concept of abstraction, where

Java cloning abstract objects

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 03:47:55
I'm wondering if there is any way to do the following. I have an abstract class, Shape , and all its different subclasses and I want to override the clone method. All I want to do in the method is create a new Shape from the toString() of the current one. Obviously I can't do the following because Shape is abstract. Is there another way to do this because overriding clone in every subclass just for a simple name change seems useless. public abstract class Shape { public Shape(String str) { // Create object from string representation } public Shape clone() { // Need new way to do this return

static abstract methods in c++

为君一笑 提交于 2019-11-29 03:05:53
I have an abstract base class class IThingy { virtual void method1() = 0; virtual void method2() = 0; }; I want to say - "all classes providing a concrete instantiation must provide these static methods too" I am tempted to do class IThingy { virtual void method1() = 0; virtual void method2() = 0; static virtual IThingy Factory() = 0; }; I know that doesnt compile, and anyway its not clear how to use it even if it did compile. And anyway I can just do Concrete::Factory(); // concrete is implementation of ITHingy without mentioning Factory in the base class at all. But I feel there should be

How can I make an “abstract” enum in a .NET class library?

百般思念 提交于 2019-11-29 02:48:46
问题 I'm making a server library in which the packet association is done by enum. public enum ServerOperationCode : byte { LoginResponse = 0x00, SelectionResponse = 0x01, BlahBlahResponse = 0x02 } public enum ClientOperationCode : byte { LoginRequest = 0x00, SelectionRequest = 0x01, BlahBlahRequest = 0x02 } That works fine when you're working in your own project - you can compare which enum member is returned (i.e. if (packet.OperationCode == ClientOperationCode.LoginRequest) ). However, since

Can I create Java-like interfaces in Perl?

余生长醉 提交于 2019-11-29 02:44:59
问题 I understand that Perl's OO model is rather primitive; it is, in most respects, essentially a namespace hack. Nevertheless, I wonder if it is possible to create something like an "interface?" My goal is to have a base class from which others are extended whose principal purpose is to make mandatory the implementation of certain methods (by name is fine, no signature necessary) by those subclasses. I don't really care if it's a "purely virtual" class (like an "interface" in Java) or a concrete