Java Interview Questions | Page 19


Question 181:

What is the difference between Executor.submit() and Executor.execute() method?

Both submit() and execute methods are used to submit a task to Executor framework for asynchronous execution. Below are some difference between them - 

Executor.submit() -

  • submit() method accepts both Runnable and Callable tasks.
  • submit() method returns Future object, which can be used to check result of submitted task.
  • This(Future) object can also be used to to cancel the task prematurely, with the cancel method.

Executor.execute() -

  • execute() method does not return anything. 
  • execute() method accepts Runnable task only.

 

Question 182:

Will this code compile?

abstract class AbstractClass{
    public void mymethod() {
        System.out.print("Abstract");
    }
}

class poly {
    public static void main(String a[]) {
        AbstractClass m = new AbstractClass() {};
        m.mymethod();
    }
}

Yes, the above code will compile successfully. 

AbstractClass m = new AbstractClass(){};

In the above declaration, we are creating an instance of an anonymous subclass of "AbstractClass" abstract class.

Question 183:

What is the difference between ArrayList and CopyOnWriteArrayList?

Question 184:

What is the difference between shutdownNow() and shutdown() in Executor Framework in java?

Question 185:

What is the disadvantage of Runnable interface over Callable interface?

There are two disadvantages with using the Runnable interface in JAVA over Callable interface:

  1. run() method cannot return value from method run.
  2. run() method cannot throw Checked Exception.

Question 186:

Can we override Object class methods within an interface in Java?

According to Javadocs,

The members of an interface are those which are declared in the interface and those which are inherited from direct super interfaces. If an interface has no direct super interface then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in the Object class, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by that interface. This is what makes the signatures of the Object methods available to the compiler and the code compiles without any error. Remember if the interface tries to declare a public instance method declared 'final' in the Object class then it'll result in a compile-time error.

For example -  'public final Class getClass()' is a public instance method declared 'final' in the Object class and therefore if an interface tries to declare a method with this signature then the compilation will fail.

Source - https://stackoverflow.com/questions/8721848/overriding-object-class-methods-within-an-interface-in-java

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media