[Java] Interface

Back-End/Java_1 2016. 9. 12. 14:00
donaricano-btn

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

1
2
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
블로그 이미지

리딩리드

,