What is an Interface?
A Java Interface can contain only method declarations and/or public static final constants, which doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present in the Interface. Also Interfaces have the ability to extend from other Interfaces(multiple Inheritance) and cannot be instantiated with the new operator.
What is an Abstract Class (ABC)?
Abstract class must be extended/subclassed in order to be implemented (useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. i.e. A Vehicle class would be an ABC, while its subclasses Car, Truck, FourWheeler, Spacecraft, etc are specific implementations.
What are the similarities between an ABC and an Interface?
•There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an Interface.
•Neither an ABC nor an Interface can be instantiated.
What are the main differences between an ABC and an Interface?
•An Interface can only contain public members, whereas an ABC can contain private as well as protected members.
•All variables in an Interface are by default, public static final, while an ABC can have instance variables.
•A class that implements an Interface must implement all methods defined in that Interface, while a class extending an ABC need not implement any of the methods defined in an ABC.
When is an Interface preferred over an ABC?
Interfaces are preferred in situations when all its properties need to be implemented by subclasses.
When is an ABC preferred over an Interface?
Abstract classes are preferred in situations when some general methods should be implemented and specialization behavior should be implemented by subclasses.
When should you tend towards an ABC or towards an Interface?
If the various objects are all of the same kind, and share a common state and behavior, then tend towards a common base class. If all they share is a set of method signatures, then tend towards an Interface.
What is the down side to an ABC and an Interface?
The downside to an Interface is; if you want to add a new feature (method) in its contract, then you MUST implement the method in all of the classes which implement that Interface. However, in the case of an ABC, the method can be simply implemented in the ABC and the same can be called by its subclass.
Why should you separate Interfaces from implementation?
Interfaces are the company’s most valuable resources – they’re expensive.
Can Interfaces be used to describe the “peripheral abilities” of a class?
Yes, Interfaces are often used to describe the peripheral abilities of a class, and not its central identity. i.e. An Automobile class might implement the Paint interface, which could apply to many otherwise totally unrelated objects.
What must a class do to implement an Interface?
The class must provide all the methods in the interface and identify the interface in its implements clause.
What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.
How to define an Interface?
In Java an Interface defines the methods but does not implement them and can include constants. A class that implements Interface is bound to implement all the methods defined in Interface.
Example of Interface:
public interface SampleInterface{
public void sampleFunction();
public int SAMPLE_CONSTANT = 1;
}
How to define an ABC?
An ABC serves as a template. An ABC must be extended/subclassed for it to be implemented. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. An ABC is a class that provides some general functionality but leaves specific implementation to its inheriting classes.
Example of an ABC:
abstract class SampleABC{
protected String name;
public String getName() {
return name;
}
public abstract void sampleFunction();
}
Explain in your own words the "bottom line" benefits of the use of an interface.
The interface makes it possible for a method in one class to invoke methods on objects of other classes, without the requirement to know the true class of those objects, provided that those objects are all instantiated from classes that implement one or more specified interfaces. In other words, objects of classes that implement specified interfaces can be passed into methods of other objects as the generic type Object, and the methods of the other objects can invoke methods on the incoming objects by first casting them as the interface type.
Thursday, September 27, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment