Ở 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
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 public class Example1{ public void display1(){ System.out.println("method ex1"); } } public abstract class Example2 extends Example1{ abstract void display2(); } public class Example3 extends Example2{ public void display3(){ System.out.println("method ex3"); } } 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 public interface Example1{ public void display1(); } public interface Example2 { public void display2(); } public interface Example3 extends Example1,Example2{} public class Example4 implements Example3{ public void display1(){ System.out.println("method ex1"); } public void display2(){ System.out.println("method ex2"); } } 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) public abstract class Example1 { abstract void display1(); public void display2(){ System.out.println("method" ex2); } } public class Example2 extends Example1{ public void display1(){ System.out.println("method ex1"); } } 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 public interface Example1{ public abstract void display(); } public class Example2 implements Example1{ public void display(){ System.out.println("method dispaly="); } } 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 public abstract class Example1{ protected abstract void display1(); public abstract void display2(); public abstract void display3(); } 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"); } } 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 public interface Example1{ void display1(); } public class Example2 implements Example1{ public void display1(){ System.out.println("method ex1"); } public void display2(){ System.out.println("method ex2"); } } 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 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); } } public Example2 extends Example1{ public void display2(){ System.out.println("Num2="+num_2); System.out.println("Num3="+num_3); } } 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 public interface Example1{ int num_1=10; } public class Example2 implements Example1{ public void display1(){ System.out.println("Num1="+num_1); } } 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 MailChú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.