Constructors in Java

          CONSTRUCTOR PROPERTIES

A Constructor cannot be:

abstract, static, final, native, strictfp, or synchronized".

Abstract:

If a constructor is abstract, it cannot be implemented (cannot have body or no code). A constructor should have a body as constructor gives properties to an object at the time of creation itself.

Static:

Constructors are called when an object is created but cannot be called like a method (like d1.show()) with an object; so no necessity for a constructors to be static.

Final:

Constructors are not inherited; only members (variables and methods) are inherited. So declaring a constructor final does not have any meaning as constructors cannot be overridden.


When a class is inherited, the subclass inherits the methods and variables of super class but not constructors. For this reason, constructors are not members of class; only variables and methods are members of a class. As members are inherited, they are permitted by subclass to override with its own functionality.
When constructors are not inherited, no meaning of overriding the constructors. In Java, final is used by super class not to inherit its members by subclass. When constructor is not inherited (and subclass cannot override), then why a constructor should be declared final. Already, internally a constructor property is final. Why again, final is required. For this reason, constructor cannot be declared final. If final, compiler raises error.

Finally to say, a constructor cannot be final as it is never inherited.

Syncronized:

A constructor should not be synchronized as it locks the object in creation and thereby, as long as the object is not created no other object can be instantiated .

---------------------------------------------------------------------------------------------------------------

An "Interface" cannot have a constructor because interface cannot have any contrete methods in it . But a "abstract class" can have because abstract class accpets concrete methods.

Constructor cannot return a value.

A Constructor can be overloaded..
⇒A Constructor can be Private. ---------------------------------------------------------------------------------------------------------------

Can you make Private Constructor in Java?


Yes, a constructor can be private and a private constructor can be overloaded also.

Programmer can achieve two functionalities with a private constructor.
1. Composition (or has-a relationship) is not possible with default constructor. That is, you cannot create an object of the class in another class with default constructor.

2. The class with private constructor is not fit for inheritance. That is, the class cannot be extended.

Can you make class not to be extended by another without declaring it final?
Yes, simple, declare the constructor as private.
---------------------------------------------------------------------------------------------------------------









Comments

Popular posts from this blog

FC Barcelona vs Real Madrid -El Clasico Rivalry.

1NF,2NF, 3NF Normalization

Memory Management (Contiguous and Non-Contiguous ).