© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

Home Placements Interview Questions Java Interview Questions And Answers Java Interview Questions On Constructor and Method ▼

Java Interview Questions On Constructor and Method

1. When does the compiler supply a default constructor for a class?

Answer
The compiler supplies a default constructor for a class if no other constructors are provided.

2. What is the difference between a constructor and a method?

Answer
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

3. Can I have multiple main methods in the same class?

Answer
No the program fails to compile. The compiler says that the main method is already defined in the class.

4. Where and how can you use a private constuctor?

Answer
Private constructor can be used if you do not want any other class to instanstiate the object , the instantiation is done from a static public method, this method is used when dealing with the factory method pattern when the designer wants only one controller (fatory method ) to create the object

5. What is an abstract method?

Answer
An abstract method is a method whose implementation is deferred to a subclass. Or, a method that has no implementation (an interface of a method).

6. What is a static method?

Answer
A static method is a method that belongs to the class rather than any object of the class and doesn't apply to an object or even require that any objects of the class have been instantiated.

7. What is a protected method?

Answer
A protected method is a method that can be accessed by any method in its package and inherited by any subclass of its class.

Next
1 2
SLogix Student Projects
bottom