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. 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. Such a class still can have member elements which can be inherited and hence facilitate code reuse. Moreover Abstract class can have non final static elements whereas interfaces are only allowed to have static final elements.
Help us improve. Please let us know the company, where you were asked this question :
a. Member elements and Methods. b. Static Variables and Static Methods. c. Static Final Variables and Instance Method Declarations. d. Member Elements , Instance Methods, Static variables and Static Methods.
Ans. Static Final Variables and Instance Method Declarations.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Abstract classes can take care of that to a certain extent. Though they are little heavier than Interfaces but An abstract class with all abstract methods and no instance variables will be able to help with everything that currently an interface does.
The only problem is that a class can only extend one class whereas it can implements multiple interfaces and that is the reason Interfaces were introduced in Java, i.e to get over the problem of multiple inheritance.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Default methods results in multiple inheritance of behavior and not of state. In case we try to implement multiple interfaces with default method having same name and signature, and don't override it in implementation class, it will throw an error.
For example -
interface MyInterface {
public void default myMethod(){
}
}
interface MyInterface2 {
public void default myMethod(){
}
}
class MyClass implements MyInterface,MyInterface2 {
}
This code will compilation error "Duplicate Default Method"
if we specify the definition of myMethod() in myClass, compiler won't complain and there is no conflict and MyClass can use overridden definition. But if we don't override myMethod() in MyClass, Java would be in conflict as to what definition should be carried to MyClass and hence throws compilation error.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes, a Class is supposed to define all abstract methods declared in the interface. With Java 8 , Interfaces can have default methods which need not be implemented by the implementing class.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Executor - Simple Interface that supports launching new tasks.
ExecutorService - Subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the Executor itself.
ScheduledExecutorService - Subinterface of ExecutorService, supports future and-or periodic execution of tasks.
Help us improve. Please let us know the company, where you were asked this question :