Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 398
Harishankaran Avatar asked Feb 01 '14 11:02

Harishankaran


2 Answers

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.

like image 104
Sean Avatar answered Oct 11 '22 15:10

Sean


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.

like image 29
Shiva Saurabh Avatar answered Oct 11 '22 16:10

Shiva Saurabh