What is the difference between a constructor and a method?
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. It 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.
What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.
What is an abstract class?
Abstract class must be extended/subclassed (to be useful). It served as a template. A class that is abstract may not be instantiated(you may not call its constructor), abstract class may contain static data.
Any class with abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.
What is the difference between an Interface and an abstract class?
An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract.
An Interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected etc.), but has some abstract methods.
What are Checked and UnChecked Exception?
A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown.
Example: IOException thrown by java.io.FileInputStream’s read() method.
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn’t force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown.
Example: StringIndexOutOfBoundsExceptio n thrown by String’s charAt() method. Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.
No comments:
Post a Comment