Set - 2

Question 26 :

What is the difference between interface and abstract class?

Answer :

interface contains methods that must be abstract; abstract class may contain concrete methods. interface contains variables that must be static and final; abstract class may contain non-final and final variables. members in an interface are public by default, abstract class may contain non-public members. interface is used to "implements"; whereas abstract class is used to "extends". interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance. interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces. interface is absolutely abstract; abstract class can be invoked if a main() exists. interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces. If given a choice, use interface instead of abstract class.


Question 27 :

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.


Question 28 :

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.


Question 29 :

What is the difference between a static and a non-static inner class?

Answer :

A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.


Question 30 :

What is an object's lock and which object's have locks?

Answer :

An object's lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock. All objects and classes have locks. A class's lock is acquired on the class's Class object.