I'm beginner in C# & EF. I got a silly doubt while designing my new sample aplication. Can an abstract inherits another abstract class
using System;
using System.ComponentModel.DataAnnotations;
namespace Spark.BusinessObjects.Common
{
public abstract class BOBase
{
public virtual int UpdatedBy { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime UpdatedOn { get; set; }
}
public abstract class MasterBOBase : BOBase
{
public virtual int CreatedBy { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime CreatedOn { get; set; }
}
}
namespace Spark.BusinessObjects.UserManagement
{
using System;
using System.Data.Linq.Mapping;
using System.ComponentModel.DataAnnotations;
using Spark.BusinessObjects.Common;
[Table(Name = "tblUser")]
public class User:BOBase
{
[Key]
public virtual int UserID { get; set; }
[StringLength(maximumLength:40,MinimumLength=10)]
[DataType(DataType.EmailAddress)]
public virtual string EMailID { get; set; }
[StringLength(maximumLength: 20, MinimumLength = 6)]
[DataType(DataType.Password)]
public virtual string Password { get; set; }
[StringLength(maximumLength: 20, MinimumLength = 3)]
public virtual string FirstName { get; set; }
[StringLength(maximumLength: 20, MinimumLength = 1)]
public virtual string LastName { get; set; }
[DataType(DataType.Date)]
public virtual DateTime DOB { get; set; }
}
public class UserRole:MasterBOBase
{
[Key]
public virtual int UserID { get; set; }
public virtual int UserRoleName { get; set; }
}
}
I don't need CreatedBy & CreatedOn properties in my master tables.Can i use like above. Is it correct?
Sure, an abstract class can inherit from another class!!
The only time a class cannot inherit from another is when the class you wish to inherit from is sealed or has private constructors only.
Yes you can inherit an abstract class from another abstract class.
You can find a example below:
public abstract class BaseClass1
{
public abstract string Method();
}
public abstract class BaseClass2 : BaseClass1
{
}
public class UserClass : BaseClass2
{
string name;
public override string Method()
{
return name;
}
}
If you want to use the method Method()
in BaseClass2 also you can override the method from BaseClass1
in BaseClass2 you can putpublic override abstract string Method();
within in, but with or without it the intended action is moreover the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With