Interface
- interface is a blueprint of a class
- It has static constants and abstract methods only
- It is used to achieve fully abstraction and multiple inheritance in java
1. Why use it
- It is used to achieve fully abstraction
- By interface, we can support the functionality of multiple inheritance
- It can be used to achieve loose coupling
- The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members
2. Example
interface
printable{
void
print();
}
class
A
implements
printable{
public
void
print(){
System.out.println(
"Hello"
);
}
}
public
class
Test{
public
static
void
main(String[]args){
A obj =
new
A();
obj.print();
}
}
3. Multiple inheritance
interface
printable{
void
print();
}
interface
showable{
void
show();
}
class
A
implements
printable, showable{
public
void
print(){
System.out.println(
"Hello"
);
}
public
void
show(){
System.out.println(
"Show"
);
}
}
public
class
Test{
public
static
void
main(String[]args){
A obj =
new
A();
obj.print();
obj.show();
}
}
1) Is it possible by interface, why?
- There is no ambiguity as implementation is provided by the implementation class
4. What is marker or tagged interface?
- An interface that have no member is known as marker or tagged interface
- Serializable, Cloneable, Remote....
- They are used to provide some essential information to the JVM so that JVM may perform some useful operation
public
interface
Serializable{
}
'Back-End > Java_1' 카테고리의 다른 글
[Java] Encapsulation (0) | 2016.09.12 |
---|---|
[Java] Package (0) | 2016.09.12 |
[Java] Abstract class (0) | 2016.09.12 |
[Java] Instanceof (0) | 2016.09.09 |
[Java] Static Binding and Dynamic Binding (0) | 2016.09.08 |