Interface
|
1. What is interface?
•
An interface is similar to a class, but the body of an interface can
include only abstract methods and final fields (constants).
•
An interface is not a class.
Writing an interface is similar to writing a class, but they are two different
concepts. A class describes the attributes and behaviors of an object. An
interface contains behaviors that a class implements.
•
Unless the class that
implements the interface is abstract, all the methods of the interface need to
be defined in the class.
An interface is similar to a class in the
following ways:
•
An interface can contain any
number of methods.
•
An interface is written in a
file with a .java extension,
with the name of the interface matching the name of the file.
•
The byte code of an interface
appears in a .class file.
•
Interfaces appear in
packages, and their corresponding byte code file must be in a directory
structure that matches the package name.
However, an interface is different from a class
in several ways, including:
•
You cannot instantiate an
interface.
•
An interface does not contain
any constructors.
•
All of the methods in an
interface are abstract.
•
An interface cannot contain
instance fields. The only fields that can appear in an interface must be
declared both static and final.
•
An interface is not extended
by a class; it is implemented by a class.
•
An interface can extend
multiple interfaces.
Declaring Interfaces:
•
The interface keyword is used to declare an interface.
Example:
public interface InterfaceName
{
//Any number of
final, static fields
//Any number of
abstract method declarations\
}
Interfaces have the following properties:
•
An interface is implicitly
abstract. You do not need to use the abstract keyword
when declaring an interface.
•
Each method in an interface
is also implicitly abstract, so the abstract keyword is not needed.
•
Methods in an interface are
implicitly public.
When overriding methods defined in interfaces
there are several rules to be followed:
•
Checked exceptions should not
be declared on implementation methods other than the ones declared by the
interface method or subclasses of those declared by the interface method.
•
The signature of the
interface method and the same return type or subtype should be maintained when
overriding the methods.
•
An implementation class
itself can be abstract and if so interface methods need not be implemented.
•
A class can implement more
than one interface at a time.
•
A class can extend only one
class, but implement many interfaces.
•
An interface can extend
another interface, similarly to the way that a class can extend
Extending Multiple Interfaces:
•
A Java class can only extend
one parent class. Multiple inheritance is not allowed. Interfaces are not
classes, however, and an interface can extend more than one parent interface.
•
The exteds keyword is used
once, and the parent interfaces are declared in a comma-separated list.
Example:
public interface Hockey
extends Sports,Event
Tagging Interfaces:
Adds
a data type to a class:
•
This situation is where the
term tagging comes from. A class that implements a tagging interface does not
need to define any methods (since the interface does not have any), but the
class becomes an interface type through polymorphism.
2. What is difference between interface and abstract class?
•
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
|
abstract Classes
|
Interfaces
|
1
|
abstract class can extend only one class or one abstract
class at a time
|
interface can extend any number of interfaces at a time
|
2
|
abstract class can extend from a class or
from an abstract class
|
interface can extend only from an interface
|
3
|
abstract class can have
both abstract and concrete methods
|
interface can have only abstract methods
|
4
|
A class can extend only one abstract class
|
A class can implement any number of interfaces
|
5
|
In abstract class keyword ‘abstract’ is mandatory to
declare a method as an abstract
|
In an interface keyword ‘abstract’ is optional to
declare a method as an abstract
|
6
|
abstract class can have protected , public
and public abstract methods
|
Interface can have only public abstract methods i.e. by
default
|
7
|
abstract class can have static, final or
static final variable with any access specifier
|
interface can have only static final
(constant) variable i.e. by default
|
3. When we should go for interface and abstract class and
concrete class?
When
to use abstract class and interface in Java
Here are some guidelines on when to use an
abstract class and when to use interfaces in Java:
•
An abstract class is good if you think you
will plan on using inheritance since it provides a common base class implementation
to derived classes.
•
An abstract class is also good if you want to
be able to declare non-public members. In an interface, all methods must be
public.
•
If you think you will need to add methods in
the future, then an abstract class is a better choice. Because if you add new
method headings to an interface, then all of the classes that already implement
that interface will have to be changed to implement the new methods. That can
be quite a hassle.
•
Interfaces are a good choice when you think
that the API will not change for a while.
•
Interfaces are also good when you want to have
something similar to multiple inheritance, since you can implement multiple
interfaces
4. What modifiers applicable for interfaces?
Because:
•
All methods and fields in
an interface should be declared in such a way so that they can be
invoked from anywhere. Not only from within a subclass. Only public modifier can make this
happen.
•
An interface simply defines
an external API without providing any implementation. The whole idea behind an
interface is that the implementation is left entirely to the implementing
class.
5. Explain about
interface variables and what modifiers are applicable for them?
6. Explain about
interface methods and what modifiers are applicable for them?
7. Can java class
implement any number of interfaces?
8. If two interfaces
contains a method with same signature but different return types, then how we
can implement both interfaces simultaneously?
•
There is nothing to identify.
Interfaces only prescribe a method name and signature. If both interfaces have
a method of exactly the same name and signature, the implementing class can
implement both interface methods with a single concrete method.
•
However, if the semantic contracts of the two
interface method are contradicting, you've pretty much lost; you cannot
implement both interfaces in a single class then.
•
If a type implements two
interfaces, and each interface define a method that has identical signature, then in
effect there is only one method, and they are not distinguishable.
•
If, say, the two methods have conflicting
return types, then it will be a compilation error. This is the general rule of
inheritance, method overriding, hiding, and declarations, and applies also to
possible conflicts not only between 2 inherited interface methods, but also
an interface and a super class method, or even just conflicts due to type erasure of
generics
9. Difference between
extends and implements key word?
•
Although, Implements and
Extends are two keywords that provide a mechanism to inherit attributes and
behavior to a class in Java programming language, they are used for two
different purposes.
•
Implements keyword is used for
a class to implement a certain interface, while Extends keyword is used for a
subclass to extend from a super class.
•
When a class implements an
interface, that class needs to implement all the methods defined in the
interface, but when a subclass extends a super class, it may or may not
override the methods included in the parent class.
•
Finally, another key
difference between Implements and Extends is that, a class can implement
multiple interfaces but it can only extend from one super class in Java.
•
In general, usage of
Implements (interfaces) is considered more favorable compared to the usage of
Extends (inheritance), for several reasons like higher flexibility and the
ability to minimize coupling. Therefore in practice, programming to an
interface is preferred over extending from base classes.
10. We cannot create an object of abstract class then what is
necessity of having constructor inside abstract class?
You would define a constructor in an abstract class if you
are in one of these situations:
•
you want to perform some initialization (to
fields of the abstract class) before the instantiation of a subclass actually
takes place
•
you have defined final fields in the abstract
class but you did not initialize them in the declaration itself; in this case,
you MUST have a constructor to initialize these fields
Note:
•
you may define more than one constructor (with
different arguments)
•
you can (should?) define all your constructors
protected (making them public is pointless anyway)
•
your subclass constructor(s) can call one
constructor of the abstract class; it may even have to call it (if there is no no-arg constructor in the
abstract class)
In any case, don't forget that if you don't define a
constructor, then the compiler will automatically generate one for you (this
one is public, has no argument, and does nothing).
Yes when we define a class to be an Abstract Class it cannot
be instantiated but that does not mean an Abstract class cannot have a
constructor. Each abstract class must have a concrete subclass which will
implement the abstract methods of that abstract class.
When we create an object of any subclass all the constructors
in the corresponding inheritance tree are invoked in top to bottom approach.
Same case applies to abstract classes. Though we cannot create an object of
abstract class, when we create an object of a class which is concrete and
subclass of the abstract class, constructor of the abstract class is
automatically invoked. Hence we can have a constructor in abstract classes.
Note : A non-abstract class cannot have an abstract methods
but an abstract class can have a non-abstract method. Reason is similar to that
of constructors, difference being instead of getting invoked automatically we
can call super(). Also there is nothing like abstract constructor as it makes
no sense at all.
note on saying ...
constructor of the abstract class is automatically invoked ... that
this is only true for the default constructor of the abstract class, others
would have to be explicitly invoked through super(args)
Non-default constructors can be automatically invoked,
provided that they are no-arg constructors. So it is not only true for default
constructors
11. What is marker
interface give an example?
•
Marker interface is used as a tag to
inform a message to the java compiler so that it can add special behavior
to the class implementing it. Java marker interface has no members in it.
•
From java 1.5, the need for marker interface
is eliminated by the introduction of the java annotation feature. So, it is
wise to use java annotations than the marker interface. It has more feature and
advantages than the java marker interface.
•
We cannot
create marker interfaces, as you cannot instruct JVM to add special behavior to
all classes implementing (directly) that special interface.
Examples
•
java.lang.Cloneable
•
java.io.Serializable
•
java.util.EventListener
12. What is adapter
class and explain its usage?
13. An interface
contains only abstract methods and an abstract class also can contain only
abstract methods then what is necessity of interface?
•
Java doesn't support multiple class
inheritance. So you can implement multiple interface. More importantly you can
implement interface and extend other class at the same time. If you used
abstract class in place of interface which would not be possible.
•
If a class implements an
interface with constants, then the class can refer to those constants without a
qualifying class name
14. in your previous
project where you used the following marker interface, abstract class,
interface, adapter class?