abstract-class

Hibernate: “InstantiationException: Cannot instantiate abstract class or interface”

我的未来我决定 提交于 2020-01-15 03:29:11
问题 I have the following class hierachy: public class MailAccount{ IncomingMailServer incomingServer; OutgoingMailServer outgoingServer; } public class MailServer{ HostAddress hostAddress; Port port; } public class IncomingMailServer extends MailServer{ // ... } public class OutgoingMailServer extends MailServer{ // ... } public class ImapServer extends IncomingMailServer{ // ... } public class Pop3Server extends IncomingMailServer{ // ... } public class SmtpServer extends OutgoingMailServer{ //

Polymorphic ViewModel collection and rendering in MVC partial Views

折月煮酒 提交于 2020-01-14 08:07:47
问题 I'm having a problem with a polymorphic collection of ViewModels in my MVC application. I received this via a web service call and i need to iterate through them and give them their own partial view, based on the object type. public abstract class ProvinceViewModel { public string Code { get; set; } } public sealed class OntarioViewModel : ProvinceViewModel { } public sealed class QuebecViewModel : ProvinceViewModel {} In my view i am trying to iterate through them and assign a partial view.

Can I define an abstract class for all derived Singletons in this way?

三世轮回 提交于 2020-01-14 07:06:07
问题 This is my abstract class which must be derived each time I want to make a Singleton: public abstract class Singleton<T> where T : Singleton<T> { private static readonly Lazy<T> _instance = new Lazy<T>(() => { var constructor = typeof(T).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null); return (T)constructor.Invoke(null); }); public static T Instance { get { return _instance.Value; } } public Singleton() { } } So, every time I need to follow the

What is the difference between an abstract class and an interface? [duplicate]

谁都会走 提交于 2020-01-13 18:01:30
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Interface vs Abstract Class (general OO) Can I ever instantiate an Abstract class? If so, why would I not make all my non-sealed classes abstract? If I can't instantiate it, then what is the difference from an interface? Can the abstract class have "base" class functionality? Is there more to the difference between an interface and an abstract class than that? 回答1: You can't instantiate an abstract class. The

Contract class should be an abstract class

本秂侑毒 提交于 2020-01-13 08:57:09
问题 The following code gives me the warning Contract class 'FooContracts' should be an abstract class . From all the examples I've read online (e.g. http://www.infoq.com/articles/code-contracts-csharp), this should work (presumably without compiler warnings). [ContractClass(typeof(FooContracts))] public interface IFoo { void Bar(string foo); } [ContractClassFor(typeof(IFoo))] internal sealed class FooContracts : IFoo { void IFoo.Bar(string foo) { Contract.Requires(foo != null); } } I'm in Visual

How can I access an instance field in an abstract parent class via reflection?

允我心安 提交于 2020-01-13 05:46:07
问题 So, for example, StringBuilder inherits from the abstract class AbstractStringBuilder . As I understand it, StringBuilder has no fields itself (except for serialVersionUID ). Rather, its state is represented by the fields in AbstractStringBuilder and manipulated by calling super in the implementations of the methods it overrides. Is there a way via reflection to get the private char array named value declared in AbstractStringBuilder that is associated with a particular instance of

Abstract generic classes taking type parameters that are themselves derived from that class

妖精的绣舞 提交于 2020-01-13 02:46:11
问题 Do you consider it an acceptable or a bad practice to create an abstract generic class that takes as a type parameter a class that derives from itself? This allows the abstract generic class to manipulate instances of the derived class and in particular the ability to create new() instances of the derived class as necessary and can help avoid repeat code in the concrete classes that derive from it. If 'bad' what alternative do you prefer to handle such situations and how would you structure

C++ : Cannot declare field to be of abstract type

情到浓时终转凉″ 提交于 2020-01-12 06:30:23
问题 I get this error on compile -> cannot declare field M1::sc to be of abstract type I1 because the following virtual functions are pure within I1. Please help. class I1 { public: virtual void a(int dir) = 0; virtual void b() = 0; virtual void c() = 0; void a(int dir) { .... } void b() { .... } void c() { .... } }; class I2 : public I1 { public: void a(int dir) { .... } void b() { .... } void c() { .... } }; class M1 : public G1 { protected: I1 sc; public: int dir = 4; sc.a(dir); }; Complete

C++ : Cannot declare field to be of abstract type

风格不统一 提交于 2020-01-12 06:28:23
问题 I get this error on compile -> cannot declare field M1::sc to be of abstract type I1 because the following virtual functions are pure within I1. Please help. class I1 { public: virtual void a(int dir) = 0; virtual void b() = 0; virtual void c() = 0; void a(int dir) { .... } void b() { .... } void c() { .... } }; class I2 : public I1 { public: void a(int dir) { .... } void b() { .... } void c() { .... } }; class M1 : public G1 { protected: I1 sc; public: int dir = 4; sc.a(dir); }; Complete

Interfaces and Abstract classes confusion in Java with examples

99封情书 提交于 2020-01-10 07:58:56
问题 I'm having trouble understanding when to use an interface as opposed to an abstract class and vice versa. Also, I am confused when to extend an interface with another interface. Sorry about the long post, but this is very confusing. Creating shapes seems like a popular starting point. Let's say we want a way to model 2D shapes. We know that each shape will have an area. What would be the difference between the following two implementations: with interfaces: public interface Shape { public