interface Printable{
void print();
}
interface Showable{
void show();
}
class A implements Printable,Showable{
public void print(){System.out.println("Salom");}
public void show(){System.out.println("Hush kelibsiz");}
public static void main(String args[]){
A obj = new A();
obj.print();
obj.show();
}
}
Quyidagi misolda klass interfeysdan foydalanadi, interfeys esa boshqa interfeysdan nasl oladi:
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class A implements Showable{
public void print(){System.out.println("Salom");}
public void show(){System.out.println("Hush kelibsiz");}
public static void main(String args[]){
A obj = new A();
obj.print();
obj.show();
}
}
Shunindek, bir interfeys tarkibida boshqa bir interfeys bo'lishi ham mumkin:
interface Xat {
void xatKeldi();
interface xatHabar{
void xatHabarKeldi();
}
}
Abstrakt klass va interfeys o'rtasidagi farqlar
Abstrakt klass
|
Interfeys
|
Abstrakt klassda abstrakt va abstrakt bo'lmagan funksiyalar bo'ladi
|
Interfeysda faqat abstrakt funksiyalar bo'ladi
|
Classlar faqat bitta abstrakt klassdan meros olishi mumkin
|
Classlar bir nechta interfeyslardan meros olishi mumkin
|
Abstrakt klasslarda final, non-final static va non-static bo'lgan o'zgaruvchilar bo'lishi mumkin
|
Interfeysda esa faqat final va static o'zgaruvchilar bo'ladi
|
Abstrakt klaslarda static funksiyalar, main funksiyalar va konstruktorlar yaratish mumkin
|
Interfeysda esa static funksiyalar, main funksiyalar va konstruktorlar yaratib bo'lmaydi
|
Interfeys va abstrakt klasslarga misol:
//interface da 4ta method yaratilyapti
interface A {
void a(); abstract
void b();
void c();
void d();
}
// B abstrakt klassi yaratilyapti va A interfeysdan foydalanilyapti
abstract class B implements A{
public void c() {System.out.println("Men c man ");}
}
class M extends B{
public void a(){System.out.println("Men a man ");}
public void b(){System.out.println("Men d man ");}
public void d(){System.out.println("Men d man ");}
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}
}
Natija :
Men a man
Men b man
Men d man
Men c man
21-dars - Javada Collection (To’plam).
Javada to'plamlar(collection'lar) framework bo'lib, u o'zida obyektlarni saqlaydi.
To'plamlarda barcha jarayonlar ( saralash, qidirish, yozish, o'chirish va h.k lar)ni oson amalga oshirish mumkin.
To'plamlar bir necha interface'lardan tashkil topgan. Ular ( Set, List, Queue, Deque etc.) va klass'lar (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet va h.k).
To'plamlar obyektning alohida ko'rinishi.
Do'stlaringiz bilan baham: |