Java nima Java dasturlash tili


void print(); } interface


Download 1.25 Mb.
bet34/49
Sana20.01.2023
Hajmi1.25 Mb.
#1103409
1   ...   30   31   32   33   34   35   36   37   ...   49
Bog'liq
2 5420273372074344755

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 class interfeydan 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 interfyes tarkibida boshqa bir interyes bo'lishi ham mumkin:
interface Xat{
void xatKeldi();
interface xatHabar{
void xatHabarKeldi();
}
}

abstract class va interface o’rtasidagi farqlar




Abstract class

Interface

Abstract classda abstract va abstract bo’lmagan methodlar bo’ladi

Interface da faqat abstract methodlar bo’ladi

Classlar faqat bitta abstract classdan meros olishi mumkin

Classlar birnecha interfayslardan meros olishi mumkin

Abstract classlarda final, non-final static va non-static bo’lgan o’zgaruvchilar bo’lishi mumkin

Interface da esa faqat final va static o’zgaruvchilar bo’ladi

Abstract claslarda static methodlar, main methodlar va constructrlar yaratish mumkin

Interface da esa static methodlar, main methodlar va constructrlar yaratib bo’lmaydi

Interface va abstract class larga misol:

//interface da 4 ta method yaratilyapti
interface A{
void a();abstract
void b();
void c();
void d();
}
// B abstract classi yaratilyapti va A interface implement qilyapti
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


Collection

Javada Collectionlar(to’plamar) freamwork dir u o’zida obyektlarni saqlaydi.

Collection larda barcha jarayonlar (saralash, qidirish, yozish, o’chirish va.h lar)ni oson amalga oshirish mumkun.

Collection lar bir necha interface lardan tashkil topgan. Ular (Set, List, Queue, Deque etc.) va class lar (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

Collection obyektning alohida ko’rinishi.

Collection framework lar tuzulishi noma’lum bo’lgan obyet guruhlarini o’zida yig’adi. Collection lar ma’lumot yig’ishi bilan massivlarga o’xshab ketadi. Farqi masiv o’lchami satik collection larniki esa dinamik, masiv bilan collectionlarni qurulishi bir biridan tubdan faq qiladi va.k.

Collectionlarning tuzilish sxemasi:

Collectionlar java.util paketida saqlanadi.

List

List dublikat elementlarni o’z ichiga olishi mumkun. Elementlar joylashtirilgan yoki ro'yxatda o'z holatiga ruxsat etilishi mumkin.

Listdan implement qiladigan klaslar


ArrayList
LinkedList
Vector

Set

Set ham collection u o’zida dublikat elementlarni saqlamaydi. Setdan asosiy 3 ta class implement oladi ular: HashSet, TreeSet, and LinkedHashSet, HashSet elementlarni hashlab hash jadvaliga yig’adi.Treeset elementlarni daraxtsimon va saralangan holda saqlaydi saqlaydi.

Setdan implement qiladigan classlar


HashSet
LinkedHashSet
TreeSet

Map

Map key ham value si ham obyekt bo’lgan collection. U o’zida dublkat bo’lagan key larni saqlamaydi. Mapdan e ta class implement oladi HashMap, TreeMap, and LinkedHashMap. HashMap : elementlarni hashlab yozadi, TreeMap : elementlarni qiymati(value) asosida saralab yozdadi.

Mapdan implement oladigan claslar



Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   49




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling