Can an abstract class have Constructors?

Source: Internet
Author: User

Can an abstract class have Constructors?

We know that abstract classes cannot be instantiated. But can an abstract class have constructor? The answer is yes. The constructor of an abstract class is used to initialize some fields of an abstract class, which all occur before the class is instantiated. In addition, there is a clever application of the constructor of the draw class: it is the code that must be executed by sub-classes within it.

 

The following fields are initialized in the constructor of the abstract class.

    class Program
    {
        static void Main(string[] args)
        {
            Employee employee = new Employee();
            Console.WriteLine(employee.ID);
            Console.ReadKey();
        }
    }
    public abstract class Base
    {
        private Guid _id;
        public Base()
        {
            this._id = Guid.NewGuid();
        }
        public Guid ID
        {
            get { return this._id; }
        }
    }
    public class Employee : Base
    {
        
    }

 

Summary: Although abstract classes cannot be instantiated, they can have constructors. Abstract class constructor occurs before instantiating a derived class. Therefore, you can initialize the abstract class field or execute other Code related to the subclass at this stage.


C # Tell me how to use the constructor in the abstract class.

First, there is a basic concept: abstract classes cannot be instantiated. That is to say, if you declare such a class:
Public abstract class BaseClass
{
Public int someProperty = 0;
Public BaseClass (int property)
{
This. someProperty = property;
}
}
Then on the client BaseClass base = new BaseClass (1); in this way, the compiler reports an error.
The constructor of an abstract class can only be called in the constructor of its subclass. That is to say, a constructor with parameters must be called explicitly no matter whether it is an abstract class or not. In this way, the constructor of the parent class can be called in the subclass (using the base keyword ):
Public class ChildClass: BaseClass
{
Public ChildClass (int property)
: Base (property)
{

}
}

How does a java Abstract class constructor implement in a subclass?

Why is there a conflict? I don't understand what you mean after "super (with or without parameters) will call the new fatherclass (with or without parameters) to initialize.
If you think there is a conflict, let's take an example of it. Or the description is clear. If it is an excerpt from that book, do not rewrite it. Extract the original text and give a description of the title. I think you are wrong.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.