|
| 1 | +RULES: |
| 2 | + |
| 3 | + |
| 4 | + 1.A Constructor cannot have a return type. |
| 5 | + |
| 6 | + 2.A constructor must have the same name as that of the Class. |
| 7 | + |
| 8 | + 3.Constructors cannot be marked static |
| 9 | + |
| 10 | + 4.A constructor cannot be marked abstract |
| 11 | + |
| 12 | + 5.A Constructor cannot be overridden. |
| 13 | + |
| 14 | + 6.A Constructor cannot be Final. |
| 15 | + |
| 16 | +PROPERTIES: |
| 17 | + |
| 18 | + 1.A constructor is invoked when a new object is created. |
| 19 | + |
| 20 | + 2.Constructors can also be overloaded but it can not be overridden. |
| 21 | + |
| 22 | + 3.Every class has at least one constructor. If a user doesn’t provide any, JVM will provide a default no-arg constructor. |
| 23 | + |
| 24 | + 4.Abstract class also has a constructor. |
| 25 | + |
| 26 | + 5.A constructor must have the same name as the class. |
| 27 | + |
| 28 | + 6.A constructor can’t have a return type. |
| 29 | + |
| 30 | + 7.If a method with the same name as a class has return type will be treated as a normal member method and not constructor. |
| 31 | + |
| 32 | + 8.A constructor can have any access modifier(All). |
| 33 | + |
| 34 | + 9.A default constructor is a no-arg constructor which calls the no-arg constructor of the superclass. In case superclass doesn’t have any no-arg constructor then it will throw a runtime exception. |
| 35 | + |
| 36 | + 10.In a case where a class has the default constructor, its superclass needs to have a no-arg constructor. |
| 37 | + |
| 38 | + 11.The first statement of a constructor can be either this or super but cannot be both at the same time. |
| 39 | + |
| 40 | + 12.If the coder doesn’t write any this or super call then the compiler will add a call to super. |
| 41 | + |
| 42 | + 13.Instance member can be accessible only after the super constructor runs. |
| 43 | + |
| 44 | + 14.Interfaces do not have constructors. |
| 45 | + |
| 46 | + 15.A constructor is not inherited. Hence cannot be overridden. |
| 47 | + |
| 48 | + 16.A constructor cannot be directly invoked. It will be invoked(Implicitly) when a new object is created or a call by other constructors. |
0 commit comments