Interface
What is an interface according to your knowledge ?
Is it a thing that a user can see ?
or is it the thing we interact when we use various software applications and systems ?
But "Interface" in JAVA is totally different from that.
Interface is same as a class in JAVA but it is not a class.
We use this concept because we haven't any way to do multiple inheritance.
So that we can use "Interfaces" to inherit many classes.
Like we use "extends" in classes,we use the keyword "implements" in Interfaces.
Create an Interface.....
interface Human {
}
Inherit an Interface.....
eg :- class Human {
}
interface Man{
}
class Student extends Human implements Man{
}
eg :- class A{}
interface C{}
interface D{}
interface E{}
class B extends A implements C,D,E {}
Object Creation with Interface.....
class A{}
interface C{}
interface D{}
interface E{}
class B extends A implements C,D,E {}
In here interfaces can act as super classes and create objects using the sub class reference.
C c = new B ();
D d = new B ();
E e = new B ();
Although we use interfeces as "Super interface reference" , the object is created by using sub classes.
We cannot run objects with interfaces.
Therefore we cannot call the constructor.
But we can use the super keyword and call it inside the sub class and run the super interface.
Specialities.....
interface Man{
public abstract void eat();
public static final String noodles ;
}
- An interface can only contain abstract methods and they should be public.
- If we define vatrtiables, they should be modified by using public,static and final.
Comments
Post a Comment