abstract

reference to abstract class

心不动则不痛 提交于 2019-11-26 20:02:38
问题 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? 回答1: 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

Why not abstract fields?

半世苍凉 提交于 2019-11-26 19:47:21
问题 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

Abstract methods in Java

对着背影说爱祢 提交于 2019-11-26 19:23:00
问题 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? 回答1: 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

Not sure when to use an abstract property and when not

若如初见. 提交于 2019-11-26 19:08:34
问题 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 :

C++ Abstract Class: constructor yes or no?

谁都会走 提交于 2019-11-26 18:53:31
问题 A class with one (or more) virtual pure functions is abstract, and it can't be used to create a new object, so it doesn't have a constructor. I'm reading a book that provides the following example: class Employee { public: Employee(const char*, const char*); ~Employee(); const char* getFirstName() const; const char* getLastName() const; virtual double earnings() const=0 // pure virtual => abstract class virtual void print() const private: char* firstName, lastName; }; If the class is abstract

Why are C# interface methods not declared abstract or virtual?

六月ゝ 毕业季﹏ 提交于 2019-11-26 17:06:10
C# methods in interfaces are declared without using the virtual keyword, and overridden in the derived class without using the override keyword. Is there a reason for this? I assume that it is just a language convenience, and obviously the CLR knows how to handle this under the covers (methods are not virtual by default), but are there other technical reasons? Here is the IL that a derived class generates: class Example : IDisposable { public void Dispose() { } } .method public hidebysig newslot virtual final instance void Dispose() cil managed { // Code size 2 (0x2) .maxstack 8 IL_0000: nop

How can I force inheriting classes to implement a static method in C#?

坚强是说给别人听的谎言 提交于 2019-11-26 16:24:46
问题 All I want to do is make sure that child classes of the class Item implement a static method and I want this to be checked at compile time to avoid runtime errors. abstract classes with static methods don't seem to work: ERROR: A static member cannot be marked as override, virtual, or abstract public abstract class Item { public static abstract Item GetHistoricalItem(int id, DateTime pastDateTime); } public class Customer : Item { public static override Customer GetHistoricalItem(int id,

Why would one declare a Java interface method as abstract?

ぃ、小莉子 提交于 2019-11-26 15:57:48
I used the "pull interface" refactoring feature of Eclipse today to create an interface based on an existing class. The dialog box offered to create all the new methods of the new interface as "abstract" methods. What would be the benefit of that? I thought that the fact that you were allowed to declare interface methods as abstract was a superfluous and harmless feature of the language that is not particularly encouraged. Why would Eclipse support such a style, or why would someone voluntarily choose to do so? Clarification: I am not asking why interface methods are abstract, that is obvious.

Interface or abstract class?

半腔热情 提交于 2019-11-26 15:28:34
For my new Pet-Project I have a question for design, that is decided already, but I want some other opinions on that too. I have two classes (simplified): class MyObject { string name {get;set;} enum relation {get;set;} int value {get;set;} } class MyObjectGroup { string name {get;set;} enum relation {get;set;} int value {get;set;} List<MyObject> myobjects {get;set;} } Later in the Project MyObjectGroup and MyObject should be used equally. For this I could go two ways: Create an interface: IObject Create an abstract class: ObjectBase I decided to go the way of the interface, that I later in

Abstract methods in Python [duplicate]

做~自己de王妃 提交于 2019-11-26 14:59:09
问题 This question already has an answer here: Is it possible to make abstract classes in Python? 12 answers I am having trouble in using inheritance with Python. While the concept seems too easy for me in Java yet up till now I have been unable to understand in Python which is surprising to me at least. I have a prototype which follow: class Shape(): def __init__(self, shape_name): self.shape = shape_name class Rectangle(Shape): def __init__(self, name): self.shape = name In the above code how