An interface in java is a blueprint of a class. It has static constants and abstract methods only.
Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviours of an object. An interface contains behaviours that a class implements.
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.
See all concepts in code
In this image , Multiple Inheritance
Can we get an
object of Java interface?
No, we cant, but in once case yes we can
It is anonymous class. Your check class is an interface. Anonymous class defines an implementation of given interface on the fly. So it saves you from creating a separate class for Interface's implementation. This approach is only useful when you know you will never require this implementation any where else in the code.It is anonymous class. Your check class is an interface. Anonymous class defines an implementation of given interface on the fly. So it saves you from creating a separate class for Interface's implementation.
This approach is only useful when you know you will never require this implementation any where else in the code.
DIFFERENCE:--
Abstract Class | Interface |
---|---|
One abstract class cannot extend more than one abstract class | One interface can extend more than one interface |
use extends | use implements |
Can contain concrete methods & abstract methods both or any one | Must Contain only abstract methods |
Access specifier of an abstract method can be any one except private | only puclic |
you need to specify specifier and abstract | by default method is public abstract |
Abstract class can provide the implementation of interface. | Interface can't provide the implementation of abstract class. |
can have final, non-final, static and non-static variables. | only static and final variables. |
Supports multilevel and hierarchical inheritance but "not multiple inheritance" | Supports all types of inheritances – multilevel, hierarchical and multiple |
Variables may be of any access specifier including private | Variables must be public, static and final; and if omitted, taken by default |
Can not extends more than one class | can implements more than one interface |
Can use Constructor | Cannot use Constructor,because it is like method without return type,it have body |
Why Interface methods cannot be “static” & “final”?
All variables are implicitly public static and
final in interfaces.
Prior
to Java 8, you can't create static methods in interfaces. All methods are
instance methods.
A static method cannot be
overridden
Because static members and
methods are compile time elements , and In interface we use Method Overriding
and we cant override Static method because Overriding is Runtime
When we use interface
1. In general Java does not
have multiple inheritance, some times we will face some design issues regarding
multiple inheritance functionality during development in those cases it is very
Good option to use Interface.
2. If we want to provide our
services to public access then go for Interfaces.
3. Interfaces are really fit
when we want to make functionality as standardized
4. In some design scenarios
like if we want to implement some patterns interfaces are very good option ,
for example “Dependency Injection design pattern”.
5. Prefer Interfaces when ever
you want to implement decoupling designs why because interface doesn’t
contain any implementation detail by default.
When we use Abstract class .
1. If you want to
provides a common base class implementation to derived classes then use
Abstract class, for example if we have 100 derived classes want to
use same functionality then we implement that functionality in abstract class
and all the 100 derived classes can extend that abstract class , so that all
the derived classes will use.
2. If we are keep on
doing functional changes (adding new methods) to the prefer Abstract class
because the existing functionality will not break, but if you use interface in
such kind of case the existing functionality will break.
3. If we want to declare
non-public members prefer Abstract class In an interface, all methods must be
public.
4. Prefer Interfaces when ever
you want to implement coupling designs why because Abstract class contain
default implementation .
All variables are implicitly public static and
final in interfaces.
Prior
to Java 8, you can't create static methods in interfaces. All methods are
instance methods.
A static method cannot be
overridden
Because static members and
methods are compile time elements , and In interface we use Method Overriding
and we cant override Static method because Overriding is Runtime
When we use interface
If any query then make a comment
Thanks
Happy Learning :) :)
Keep Smiling :) :) :)
Thanks
Happy Learning :) :)
Keep Smiling :) :) :)
No comments:
Post a Comment