More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Earlier any class implementing an interface was supposed to implement all methods declared in an interface. There was no place for optionally implementing all or subset of methods.Though we have abstract classes wherein we could have provided such a mechanism by declaring some methods as abstract while providing definition for some. But as Abstract classes have a body and are comparatively heavier than interfaces and interfaces associate closely to the concept of providing interfacing than abstract classes, Java might have though of providing optional implementation for default methods. This way same interface can be reused in variety of ways rather than making copies of an interface to suit different needs.
Help us improve. Please let us know the company, where you were asked this question :
Ans. An interface without any method declaration is called as marker interface. there are 3 in-built interfaces in JVM i.e. serializable, clonable, remote
Help us improve. Please let us know the company, where you were asked this question :
Ans. Keep it as a Abstract Class if its a "Is a" Relationsship and should do subset/all of the functionality. Keep it as Interface if its a "Should Do" relationship.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Collections class is a utility class having static methods for doing operations on objects of classes which implement the Collection interface. For example, Collections has methods for finding the max element in a Collection.
Help us improve. Please let us know the company, where you were asked this question :
Q40. What will happen if class implement two interface having common method?
Ans. That would not be a problem as both are specifying the contract that implement class has to follow. If class C implement interface A & interface B then Class C thing I need to implement print() because of interface A then again Class think I need to implement print() again because of interface B, it sees that there is already a method called test() implemented so it's satisfied.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Cloneable is a declaration that the class implementing it allows cloning or bitwise copy of it's object state. It is not having any method because it is a MARKER interface.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism.
Help us improve. Please let us know the company, where you were asked this question :
Ans. An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It allows sequential access to all the elements stored in the collection.
Help us improve. Please let us know the company, where you were asked this question :
Q51. Can we have default method with same name and signature in the derived Interface as the static method in base Interface and vice versa ?
Ans. Yes , we can do that as static methods are not accessible using references and hence cannot lead to conflict. We cannot do inverse as Default methods cannot be overridden with the static methods in derived interface.
Help us improve. Please let us know the company, where you were asked this question :
Ans. This is an informative annotation that specify that the interface is a functional interface. A Function Interface has only one abstract method and many default methods. Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Abstract classes can have both abstract methods ( method declarations ) as well as concrete methods ( inherited to the derived classes ) whereas Interfaces can only have abstract methods ( method declarations ).
A class can extend single abstract class whereas it can implement multiple interfaces.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It's a choice to be made whether to use first approach ( Thread class ) or second approach ( runnable interface ) by the programmer. The second facility has been given for cases where your class is already extending some parent class and hence cannot extend another class ( for Thread ) as Java doesn't allow multiple inheritance.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Thread class holds the definition of start method ( This is the method that starts execution of new thread and then calls run method within the scope of new thread ). Interfaces don't hold any definition and so does runnable. So it makes it necessary the usage of Thread class , whatever implementation you choose.
When your class extends the thread class, it carries the definition of start method from parent Thread class onto itself and hence new yourClass.start() helps starting a new thread and then executing run method in that new thread scope.
When you implement runnable interface , you are just making it sure to the JVM that you have implemented the required method ( run() ) which the Thread start method will look for upon executing start method.
Help us improve. Please let us know the company, where you were asked this question :