# INTERFACE #
* In real world, interface is a communication channel by which two unrelated objects can interact with each other, like a human being can interact with a computer with operating system.
* In Java, interface is like a blueprint (set of guidelines) by which we can create some unrelated classes those are doing some related task.
* The interface can be declared using interface keyword.
example:
public interface Foo
{
}
# Features of interface #
* We can not create object of iterface, but their reference variable can be created.
example:
Foo f = new Foo(); <---- it is not valid
Foo f; <---- valid
Foo f = null; <---- valid
* Interfaces may have following contents
1. public static final type variables <---- also known as constants
2. public abstract methods <---- methods with no body
3. public non static methods with body <---- having default keyword
4. public static methods with body
5. private non static method with body
6. private static method with body
note:
----> body means implementation of method
----> feature number 3 and 4 was added to Java in java 8
----> feature number 5 and 6 was added in java 9
* feature example:
interface OO
{
int p = 1; <---- by default it is public, static, and final
void q(); <---- by default it is public abstract
default void r() <---- by default it is public
{
}
static void s() <---- by default it is public and static
{
}
}
* Unlike classes interface does not have constructors, reason they do not have non static variables.
* The static variable and static method of interface can be accessed using interface name
example:
----> variable 'p' of interface 'OO' can be accessed as OO.p
----> method 's' of interface 'OO' can be accessed as OO.s()
* In order to access a default non static method of interface we need follwoing guidelines ---->
step-1. create a class that implement that interface (implement means following guidelines given by interface)
step-2. assign the object of class inside the reference variable of interface (bole toh polymorphic assignment)
step-3. call the method on this reference variable
example:
class MyClass implements MyInterface <--- step:1
{
public static void main(String[] args)
{
MyInterface ref = new MyClass(); <---- step:2
ref.r(); <---- step:3
}
}
* In order to access an abstract method of interface we need to follow given guidelines ---->
step-1. create a class that implement that interface (implement means following guidelines given by interface)
step-2. override the abstract method of interface <---- do not forget to apply the public modifier during overriding
step-3. assign the object of class inside the reference variable of interface (bole toh polymorphic assignment)
step-4. call the method on this reference variable
example:
class MyClass implements MyInterface <--- step:1
{
public void q() <---- step:2, look i have used public modifier on method
{
}
public static void main(String[] args)
{
MyInterface ref = new MyClass(); <---- step:3
ref.r(); <---- step:4
}
}
note: it is mandatory for the class to override all the abstract methods of interface, otherwise the class need to be declared abstract
example:
class MyClass implements MyInterface <---- the MyClass need to be declared abstract since it does not override the abstract method 'r()'
{
}
* In real world, interface is a communication channel by which two unrelated objects can interact with each other, like a human being can interact with a computer with operating system.
* In Java, interface is like a blueprint (set of guidelines) by which we can create some unrelated classes those are doing some related task.
* The interface can be declared using interface keyword.
example:
public interface Foo
{
}
# Features of interface #
* We can not create object of iterface, but their reference variable can be created.
example:
Foo f = new Foo(); <---- it is not valid
Foo f; <---- valid
Foo f = null; <---- valid
* Interfaces may have following contents
1. public static final type variables <---- also known as constants
2. public abstract methods <---- methods with no body
3. public non static methods with body <---- having default keyword
4. public static methods with body
5. private non static method with body
6. private static method with body
----> body means implementation of method
----> feature number 3 and 4 was added to Java in java 8
----> feature number 5 and 6 was added in java 9
* feature example:
interface OO
{
int p = 1; <---- by default it is public, static, and final
void q(); <---- by default it is public abstract
default void r() <---- by default it is public
{
}
static void s() <---- by default it is public and static
{
}
}
* Unlike classes interface does not have constructors, reason they do not have non static variables.
* The static variable and static method of interface can be accessed using interface name
example:
----> variable 'p' of interface 'OO' can be accessed as OO.p
----> method 's' of interface 'OO' can be accessed as OO.s()
* In order to access a default non static method of interface we need follwoing guidelines ---->
step-1. create a class that implement that interface (implement means following guidelines given by interface)
step-2. assign the object of class inside the reference variable of interface (bole toh polymorphic assignment)
step-3. call the method on this reference variable
example:
class MyClass implements MyInterface <--- step:1
{
public static void main(String[] args)
{
MyInterface ref = new MyClass(); <---- step:2
ref.r(); <---- step:3
}
}
* In order to access an abstract method of interface we need to follow given guidelines ---->
step-1. create a class that implement that interface (implement means following guidelines given by interface)
step-2. override the abstract method of interface <---- do not forget to apply the public modifier during overriding
step-3. assign the object of class inside the reference variable of interface (bole toh polymorphic assignment)
step-4. call the method on this reference variable
example:
class MyClass implements MyInterface <--- step:1
{
public void q() <---- step:2, look i have used public modifier on method
{
}
public static void main(String[] args)
{
MyInterface ref = new MyClass(); <---- step:3
ref.r(); <---- step:4
}
}
note: it is mandatory for the class to override all the abstract methods of interface, otherwise the class need to be declared abstract
example:
class MyClass implements MyInterface <---- the MyClass need to be declared abstract since it does not override the abstract method 'r()'
{
}
Very useful concepts.....
ReplyDeletejhakkas
ReplyDeleteBhaisaaaaaaaaaab
ReplyDeleteIt is good info
ReplyDeleteSir ,you are great.Very good concept
ReplyDelete