2020-10-12
  • |
  • daafoor
  • |
  • مشاهدات: 2251

الوراثة Inheritence :

تسمح لنا الوراثة بتحديد تصنيف(class) يرث جميع التوابع والخصائص من class أخر.

الكلاس الاب الرئيسي يسمى parent class او base class

الكلاس الابن يسمى Child class او derived class

 

مثال عن الوراثة :

class Bird{    
      void fly()  
         {  
            print("The bird can fly");  
          }  
   }    
      // Inherits the super class  
class Parrot extends Bird{    
         //child class function  
         void speak(){  
             print("The parrot can speak");  
                 }            
}  
void main() {  
      // Creating object of the child class  
      Parrot p=new Parrot();    
      p.speak();    
      p.fly();    
}    

 

مثال عن الوراثة في عدة مستويات :

class Bird{    
      void fly()  
         {  
            print("The bird can fly");  
          }  
   }    
      // Inherits the super class  
class Parrot extends Bird{    
         void speak(){  
             print("The parrot can speak");  
                 }  
             
}  
   
// Inherits the Parror base class  
class Eagle extends Parrot {  
          void vision(){  
             print("The eagle has a sharp vision");  
                 }  
}  
void main() {  
      // Creating object of the child class  
      Eagle e=new Eagle();    
      e.speak();    
      e.fly();    
      e.vision();  
}   

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول