Why do abstract classes have constructors




















Shaili Dashora Updated date Nov 29, Step 1 First we will create a console application named InterviewQuestionPart7. Step 2 Then we will create an abstract class named customer and include a private field, the return type of this private field is Guid that is a Globally Unique Identifier. Now in our business we have two types of customers, CorporateCustomer and SavingsCustomer. We will create two classes, CorporateCustomer class and Savings Customer class, and they will be derived from our Customer abstract class.

Now we will create an instance of the CorporateCustomer class derived from the Customer class and the Customer class has the property public ID, so it should be available on the instance of CorporateCustomer class. And similarly we will create an instance of a SavingsCustomer class and print the property using the following code. Now let's run and see the output we get. So here within the abstract class we are using the constructor to initialize the fields of the abstract class. Step 4.

We would provide a constructor for an abstract class if we want to initialize certain fields of the abstract class before the instantiation of a child class takes place. Here we have two child classes, CorporateCustomer and SavingsCustomer, derived from the abstract Customer class. Let's actually prove that the constructor of the abstract class is called before even the child class constructor. For proving that let's include the constructor in both of the derived classes with the following code.

Now we will create a breakpoint on the CorporateCustomer constructor and SavingsCustomer constructor, in the abstract class constructor and within the Main method. Now Debug the program and see what will happen. Now we press the F10 and notice that what happens is it comes to the base class constructor.

Now again we will press F10 and it executes and assigns the Globally Unique Identifier. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Java.

Yes, an abstract class can have a constructor in Java. You can either explicitly provide a constructor to the abstract class or if you don't, the compiler will add a default constructor of no argument in the abstract class. This is true for all classes and it also applies to an abstract class.

For those who want to recall what is an abstract class in Java, it's a class that can not be instantiated with the new operator or any other way. In order to use an abstract class in Java, You need to extend it and provide a concrete class.

An abstract class is commonly used to define a base class for a type hierarchy with default implementation, which is applicable to all child classes. By the way, the difference between interface and abstract class in Java is also one of the popular and tricky Java questions and should be prepared well for Java interviews.

Can we declare constructor on abstract class in Java is a follow-up of other similar Java interview questions e. Prerequisite: Interface and Abstract class in Java. A Constructor is a special member function used to initialize the newly created object. It is automatically called when an object of a class is created. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

Why interfaces can not have the constructor? An Interface is a complete abstraction of class. All data members present in the interface are by default public, static, and final. The methods inside the interface are by default public abstract which means the method implementation cannot be provided by the interface itself, it has to be provided by the implementing class. Therefore, no need of having a constructor inside the interface.



0コメント

  • 1000 / 1000