Next >>
Java Interview Question 1:
What is a class?
Class is a template for a set of objects that share common properties and a common behavior.
Java Interview Question 2:
What is Constructor?
When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors must have the same name as the class and can not return a value. The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.
Java Interview Question 3:
What is Default Constructor?
Java provides a default constructor, if a class defined by the code does not have any constructor. If you define any constructor for your class, no default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans). The access modifier (public/private/etc.) of the default constructor is the same as the class itself.
Java Interview Question 4:
How are this() and super() used with constructors?
this() is used to invoke a constructor of the same class with a different parameter list. super() is used to invoke a superclass constructor. If a constructor uses super, it must be the first statement in the body of a constructor.
No comments:
Post a Comment