Sự Khác Nhau Giữa Abstract Class Và Interface Trong Java - Deft Blog

Mục lục

  • 1 Abstract class chỉ có thể thừa kế một class hoặc một abstract class khác
  • 2 Interface có thể thừa kế một hoặc nhiều interface khác
  • 3 Abstract class có thể chứa abstract method và method thông thường(method có thân hàm)
  • 4 Interface chỉ có abstract method
  • 5 Abstract class có thể chứa protected hoặc public abstract method
  • 6 Interface chỉ có public abstract method
  • 7 Abstract class có thể chứa static, final hoặc static final biến
  • 8 Interface chỉ có public static final biến
    • 8.1 Các bài viết liên quan

Ở những bài trước, chúng ta đã biết đến tính trừu tượng, và đi song song đó là abstract class và interface để triển khai tính trừu tượng trong java.

Chúng ta cùng xem thử nó sẽ khác nhau ở điểm nào qua bảng liệt kê sau

STT Abstract class Interface
1 Abstract class chỉ có thể thừa kế một class hoặc một abstract class khác. Interface có thể thừa kế một hoặc nhiều interface khác.
2 Abstract class có thể chứa abstract method và method thông thường(method có thân hàm). Interface chỉ có abstract method.
3 Abstract class có thể chứa protected hoặc public abstract method. Interface chỉ có public abstract method.
4 Abstract class có thể chứa static, final hoặc static final biến. Interface chỉ có public static final biến.

Abstract class chỉ có thể thừa kế một class hoặc một abstract class khác

// File Example1.java public class Example1{ public void display1(){ System.out.println("method ex1"); } } // ---------------------------------------- // File Example2.java // Abstract class thừa kế concrete class public abstract class Example2 extends Example1{ abstract void display2(); } // ---------------------------------------- // File Example3.java // Abstract class thừa kế abstract class public class Example3 extends Example2{ public void display3(){ System.out.println("method ex3"); } } // ---------------------------------------- // File Main.java public class Main{ public static void main(String args[]){ Example3 obj=new Example3(); obj.display3(); } }

Output: method ex3

Interface có thể thừa kế một hoặc nhiều interface khác

// File Example1.java public interface Example1{ public void display1(); } // File Example2.java public interface Example2 { public void display2(); } // File Example3.java // Interface thừa kế 2 interface khác public interface Example3 extends Example1,Example2{} // File Example4.java public class Example4 implements Example3{ public void display1(){ System.out.println("method ex1"); } public void display2(){ System.out.println("method ex2"); } } // File Main.java public class Main{ public static void main(String args[]){ Example4 obj=new Example4(); obj.display1(); obj.display2(); } }

Output:

method ex1

method ex2

Abstract class có thể chứa abstract method và method thông thường(method có thân hàm)

// File Example1.java public abstract class Example1 { abstract void display1(); public void display2(){ System.out.println("method" ex2); } } // File Example2.java public class Example2 extends Example1{ public void display1(){ System.out.println("method ex1"); } } // File Main.java public class Main { public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); obj.display2(); } }

Output:

method ex1

method ex2

Interface chỉ có abstract method

// File Example1.java public interface Example1{ public abstract void display(); } // File Example2.java public class Example2 implements Example1{ public void display(){ System.out.println("method dispaly="); } } // File Main.java public class Main{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display(); } }

Output: display

Abstract class có thể chứa protected hoặc public abstract method

// File Example1.java public abstract class Example1{ protected abstract void display1(); public abstract void display2(); public abstract void display3(); } // File Example2.java class Example2 extends Example1{ public void display1(){ System.out.println("method ex1"); } public void display2(){ System.out.println("method ex2"); } public void display3(){ System.out.println("method ex3"); } } // File Main.java public class Main{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); obj.display2(); obj.display3(); } }

Output:

method ex1

method ex2

method ex3

Interface chỉ có public abstract method

// File Example1.java public interface Example1{ void display1(); } // File Example2.java public class Example2 implements Example1{ public void display1(){ System.out.println("method ex1"); } public void display2(){ System.out.println("method ex2"); } } // File Main.java public class Main{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); obj.display2(); } }

Output:

method ex1

method ex2

Abstract class có thể chứa static, final hoặc static final biến

// File Example1.java public abstract class Example1{ private int num_1 = 1; protected final int num_2 = 2; public static final int num_ 3 = 3; public void display1(){ System.out.println("Num1="+num_1); } } // File Example2.java public Example2 extends Example1{ public void display2(){ System.out.println("Num2="+num_2); System.out.println("Num3="+num_3); } } // File Main.java public class Main{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); obj.display2(); } }

Output:

1

2

3

Interface chỉ có public static final biến

// File Example1.java public interface Example1{ int num_1=10; } // File Example2.java public class Example2 implements Example1{ public void display1(){ System.out.println("Num1="+num_1); } } // File Main.java public class Main{ public static void main(String args[]){ Example2 obj=new Example2(); obj.display1(); } }

Output: 10

Note:

Interface thường được dùng để mô tả một tập các chức năng. Một class thừa kế một interface A, ta nói rằng class đó có thể thực hiện các năng của interface A khai báo, mà chưa cần xem code.

Ví dụ: Chúng ta có Mail là một interface và MailImpl implements Mail

public class MailImpl implements Mail

Chúng ta sẽ hiểu rằng class MailImpl sẽ có thể thực hiện được các chứng năng mà Mail khai báo.

Abstract class thường được dùng trong mối quan hệ thừa kế, vì tính linh hoạt của nó có thể chứa abstract method và method thông thường. Dựa vào đó mà chúng ta có thể định nghĩa các khuôn mẫu hay triển khai những thuộc tính, method mà các class con có thể dùng chung.

Các bài viết liên quan

  • Functional interface trong java
  • Marker interface trong java
  • Default method trong interface
  • Static method trong interface 

Từ khóa » Khác Nhau Giữa Abstract Class Và Interface Trong Java