简体   繁体   中英

can abstract class inherit another abstract class c#

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 put public override abstract string Method(); within in, but with or without it the intended action is moreover the same.

Absolutely, an Abstract class can inherit from both non-abstract classes and other abstract classes. When you want any class to inherit from another class, you will want to watch out (most of the time) for the sealed modifier.

Yes, An Abstract class can inherit from a concrete class(non-Abstract class) and can also inherit from the following-

  • Abstract Class
  • Concrete class
  • Interface

According to inheritance concept in C#, an Abstract class can inherit from only one class either it can be Abstract or Concrete class but it can inherit from multiple interface.

However, you can not inherit from a sealed class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM