Crack your interviews with these java interview questions
Today's Addition: Hi friends. Will be out from 19th Jan to 2nd Feb for the yearly vacation. No posts till then!!

Wednesday, January 9, 2008

Core Java - I

What is an interface?

interface Runnable() {
abstract void run();
}

• Interface is completely abstract.
• No partial implementation.
• Only method signatures and constants are declared.

• All constants are implicitly static final.
• All methods are implicitly abstract.
• Treat it like a function pointer table.
• A class can implement one or more interfaces.


Benefits of Interface?

• Declaring Methods or constants that one or more classes are expected to implement.
• Capturing similarities between unrelated classes without forcing a class relationship.
• Determining an objects programming interface without revealing the actual body of the class.

Abstract class?

• An abstract class is a class that is declared abstract—it may or may not include abstract methods.
• Abstract classes cannot be instantiated.
• The purpose of abstract class is to be subclassed.
• It can have zero abstract methods, and still be declared abstract.
• If even one method is abstract the whole class must be declared abstract
• An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double LocX, double LocY);
• When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Abstract Classes versus Interfaces?

• Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods.
• Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation.
• If an abstract class contains only abstract method declarations, it should be declared as an interface instead.
Multiple interfaces can be implemented by classes (similar or dissimilar) anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.
• By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

No comments: