Guaranteed Initialization Constructor


Q What is a Constructor?
Q What is Constructor Chaining ?
Q Can we call sub class constructor from super class constructor?
No. There is no way in java to call sub class constructor from a super class constructor.
Q What is the purpose of constructor?
Q What happens if you keep a return type for a constructor?
+ Ideally, Constructor must not have a return type.
+ If we use function name as class Name and we provide return type ; its not constructor.
+ It will be treat as normal function
Example

class Test{

	int Test(){
		return 11;
	}

public static void main(String[] args) {
	Test t1=new Test();
	System.out.println(t1.Test());
}
}

Q Types Of Constructor [no-args constructor]
Default constructor (no-arg constructor)
Parameterized constructor
Q can constructor make private?
Q What is Constructor Overloading?
Q Do we have destructor in java?
NO.
Internally java uses garbage collector to deallocate memory.
Q Difference between constructor and method in Java
--------------------
IMPORTANT POINTS:
--------------------
If there is no constructor in a class, 
compiler automatically creates a default constructor.

If there is constructor in class (parameterized)
then compiler doesn't add default constructor.

Comments

  1. Informative article.
    Sir, please write about topic 'copy constructor'.

    ReplyDelete

Post a Comment