java

Javada Interfeys

Javada interfeyslar orqali to’liq abstraktsiyani tashkillashtirish mumkin. Interfeys tarkibida funksiyalarning faqat nomlari bo’lishi mumkin, ularning tanasi bo’lishi mumkin emas(Java 8 dan boshlab default funksiyalarga funksiyaning bajarilish qismi yozilishi qo’shildi). Shuningdek, Interfeys tarkibida ma’lumotlar bo’lishi ham mumkin.

Interfeyslarda o’zgaruvchilar e’lon qilinmaydi unda faqat o’zgarmas qiymatlar – final qiymatlar e’lon qilinadi.

Interfeysga bitta misol keltirsak.

  1. public interface Dasta {
  2.  
  3. void ishlatilishSohasi();
  4.  
  5. void tayorlanganMatirial();
  6.  
  7. void size();
  8.  
  9. }

Dasta nomli interfeys yaratildi, unda  ishlatilishSohasi(), tayorlanganMatirial(), size() nomli funksiyalari bor.

  1. public class Pichoq implements Dasta{
  2. @Override
  3. public void ishlatilishSohasi() {
  4. System.out.println("Pichoq uchun");
  5. }
  6. @Override
  7. public void tayorlanganMatirial() {
  8. System.out.println("Yog'och");
  9. }
  10. @Override
  11. public void size() {
  12. System.out.println("15 sm");
  13. }
  14. public static void main(String[] args) {
  15. Pichoq pichoq = new Pichoq();
  16. pichoq.ishlatilishSohasi();
  17. pichoq.tayorlanganMatirial();
  18. pichoq.size();
  19. }
  20. }

Ko’rib turganingizdek klass yaratildi va Dasta interfeysidan (implement olindi) foydalanildi. Bu holatda dasta barcha dastasi bor obyektlar uchun umumiydir.

default funksiyaga misol :

  1. interface Formula {
  2. double calculate(int a);
  3. default double sqrt(int a) {
  4. return Math.sqrt(a);
  5. }
  6. }
  7. Formula formula = new Formula() {
  8. @Override
  9. public double calculate(int a) {
  10. return sqrt(a * 100);
  11. }
  12. };
  13. formula.calculate(100);     // 100.0
  14. formula.sqrt(16);

Esda tuting: Java kompilyatori interfeys tarkibidagi funksiyalarga public va abstract kalit so’zlarni, interfeys tarkibidagi ma’lumotlarga esa public, static va final kalit so’zlarni qo’shadi.

interface

Bir klass bir vaqtda bir nechta interfeyslardan foydalanishi (implementation) mumkin. Quyidagi misolga qarang:

 multiple inheritance in java

  1. interface Printable{
  2. void print();
  3. }
  4. interface Showable{
  5. void show();
  6. }
  7. class A implements Printable,Showable{
  8. public void print(){System.out.println("Salom");}
  9. public void show(){System.out.println("Hush kelibsiz");}
  10. public static void main(String args[]){
  11. A obj = new A();
  12. obj.print();
  13. obj.show();
  14. }
  15. }
  16. Quyidagi misolda klass interfeysdan foydalanadi, interfeys esa boshqa interfeysdan nasl oladi:
  17. interface Printable{
  18. void print();
  19. }
  20. interface Showable extends Printable{
  21. void show();
  22. }
  23. class A implements Showable{
  24. public void print(){System.out.println("Salom");}
  25. public void show(){System.out.println("Hush kelibsiz");}
  26. public static void main(String args[]){
  27. A obj = new A();
  28. obj.print();
  29. obj.show();
  30. }
  31. }

Shunindek, bir interfeys tarkibida boshqa bir interfeys bo’lishi ham mumkin:

  1.     interface Xat {
  2. void xatKeldi();
  3. interface xatHabar{
  4. void xatHabarKeldi();
  5. }
  6. }

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:

  1. //interface da 4ta method yaratilyapti
  2. interface A {
  3. void a(); abstract
  4. void b();
  5. void c();
  6. void d();
  7. }
  8. // B abstrakt klassi yaratilyapti va A interfeysdan foydalanilyapti
  9. abstract class B implements A{
  10. public void c() {System.out.println("Men c man ");}
  11. }
  12. class M extends B{
  13. public void a(){System.out.println("Men a man ");}
  14. public void b(){System.out.println("Men d man ");}
  15. public void d(){System.out.println("Men d man ");}
  16. }
  17. class Test5{
  18. public static void main(String args[]){
  19. A a=new M();
  20. a.a();
  21. a.b();
  22. a.c();
  23. a.d();
  24. }}

Natija :

Men a man

Men b man

Men d man

Men c man

G'ayratjon Rayimjonov
G'ayratjon Rayimjonov

G'ayratjon Rayimjonovning so'nngi maqolalari (hammasini ko'rish)

(255 marta o'qilgan, bugun 2 marta o'qildi)

O'xshash maqolalar: