Java Merosxo’rlik (Sinf/Class)

Merosxo’rlik - OOP ning eng asosiy mexanizmlaridan biri hisoblanadi. Bu Java dasturlash tilidagi shunday konsepsiyaki, biror class boshqa bir classning xususiyatlarini (maydonlari va metodlari) o’zlashtirishi mumkin. Merosxo’rlik natijasida siz mavjud class asosida yangi class yaratishingiz mumkin. Yangi classga yangi o’zgaruvchilar va metodlar qo’shish ixtiyoringiz ham bo’ladi. Merosxo’rlik natijasida mavjud koddan qayta foydalanish imkoniyati yaratiladi.

Meros olingan class super/parent class deb, meros olgan class sub/child class deb nomlanadi. Sintaksisi quyidagicha bo’ladi:

class sub-class-nomi extends super-class-nomi 
{ 
  // class tanasi 
}

Quyida merosxo’rlikka misol keltiramiz. Bunda Bike (velosiped) classi super class, MountainBike (tog’ velosipedi) sub-class, Test classi dasturni ishga tushiruvchi main metodiga ega class.

class Bicycle {

   public int shesterna;
   public int tezlik;
   
   public Bicycle(int shesterna, int tezlik)
   {
       this.shesterna = shesterna;
       this.tezlik = tezlik;
   }
   
   public void tormoz(int decrement)
   {
       tezlik -= decrement;
   }

   public void tezlikniOshir(int increment)
   {
       tezlik += increment;
   }

   public String toString()
   {
       return ("Shesternalar soni " + shesterna + "\n"
               + "Tezlik " + tezlik);
   }
}

class MountainBike extends Bicycle {

   public int orindiqBalandigi;

   public MountainBike(int shesterna, int tezlik,
                       int orindiqBalandigi)
   {
       super(shesterna, tezlik);
       this.orindiqBalandigi = orindiqBalandigi;
   }

   public void orindiqBelgilash(int orindiqBalandigi)
   {
       this.orindiqBalandigi = orindiqBalandigi;
   }

   @Override public String toString()
   {
       return (super.toString() + "\nO'rindiq balandligi "
               + orindiqBalandigi);
   }
}

public class Test {
   public static void main(String args[])
   {
       MountainBike mb = new MountainBike(3, 100, 25);
       System.out.println(mb.toString());
   }
}
Shesternalar soni 3 Tezlik 100 O'rindiq balandligi 25

Merosxo’rlik turlari

Yagona merosxo’rlik - sub-class faqatgina bitta classdan meros olishini anglatadi. Yuqoridagi kod bunga misol bo’la oladi.

Ko’p-darajali merosxo’rlik - biror class uchun super-class bo’lgan class, boshqa bir class uchun sub-class bo’lishi. Quyidagi misolga e’tibor bering.

class Class1 {
   public void print1()
   {
       System.out.println("Print 1");
   }
}

class Class2 extends Class1 {
   public void print2() { System.out.println("Print 2"); }
}

class Class3 extends Class2 {
   public void print1()
   {
       System.out.println("Print 3");
   }
}

public class Test {
   public static void main(String[] args)
   {
       Class3 obj = new Class3();
       obj.print1();
       obj.print2();
       obj.print1();
   }
}
Print 3 Print 2 Print 3

Ierarxik merosxo’rlik - biror classning birdan ortiq classlar uchun super-class bo’lishi natijasida hosil qilinadi:

class A {
   public void print_A() { System.out.println("Class A"); }
}

class B extends A {
   public void print_B() { System.out.println("Class B"); }
}

class C extends A {
   public void print_C() { System.out.println("Class C"); }
}

class D extends A {
   public void print_D() { System.out.println("Class D"); }
}

public class Test {
   public static void main(String[] args)
   {
       B obj_B = new B();
       obj_B.print_A();
       obj_B.print_B();

       C obj_C = new C();
       obj_C.print_A();
       obj_C.print_C();

       D obj_D = new D();
       obj_D.print_A();
       obj_D.print_D();
   }
}
Class A Class B Class A Class C Class A Class D