Skip to main content

Java Inheritance

Introduction to Java Inheritance 

Inheritance का ऐसा Machanisam है जिसमे एक old class से new class create किया जाता हे इस के द्वारा old class की properties को new class में प्रयोग किया जाता हे old class की properties को new class में प्रयोग करने के लिए old class की inherit करना होता है| और class को inherit करने के लिए derivation public, private and protected का प्रयोग किया जाता है| 

inheritance में old class को base class या parent class or super class कहा जाता है, जिस class की property ली जाती है| और नई class को child class या derive class या sub class कहा जाता है, जिस class के द्वारा property ली जाती है|

ऐसा करने के लिए जो class methods को access करना चाहती है उसे दूसरी class की sub class बनना होगा। Sub class बनने के लिए आपकी class को उस दूसरी class को extend करना होगा। इसे ही inheritance कहते है। 
Inheritance से आप एक ही code को बार बार लिखने की उलझन से बच जाते है। Inheritance की इस खूबी को re-usability कहते है। यानि एक ही code को बार बार अलग अलग जगह पर reuse किया जा सकता है। 
जब कोई एक class दूसरी class को inherit करना चाहती है तो वह extends keyword यूज़ करती है। और यदि आप चाहते है की आपके के द्वारा बनायीं हुई class को कोई दूसरी class inherit ना करे तो इसके लिए आप class के नाम से पहले final keyword लगा देते है। जिन classes के नाम से पहले final keyword होता है उन्हें inherit नहीं किया जा सकता है। 
कोई भी class सिर्फ एक ही class को extend कर सकती है। जब कोई class एक से अधिक classes को extend करती है तो वह multiple inheritance कहलाता है। और में आपको बताना चाहता हुँ की जावा में multiple inheritance allow नहीं है। इसकी जगह पर आप multilevel inheritance implement कर सकते है। 
जिस class को inherit किया जाता है वह super class कहलाती है। और जो class inherit करती है वह sub class कहलाती है। यँहा पर एक ध्यान देने योग्य बात ये है की sub class super class के सभी methods और variables को access नहीं कर सकती है।
जो methods public और protected declare किये हुए है उन्हें ही sub class access कर सकती है। Superclass के किसी भी private member को subclass access नहीं कर सकती है। यदि superclass ने किसी और class को extend कर रखा है तो उस class के भी सभी public और protected members को आपकी class यूज़ कर सकती है।

Types of Java Inheritance

Java में 3 तरह से inheritance को यूज़ किया जाता है। आपकी application के लिए आपको जो suitable लगे आप वही तरीका यूज़ कर सकते है। इन तीनों तरीकों के बारे में निचे दिया जा रहा है। 

SINGLE INHERITANCE 

Single inheritance में एक class किसी दूसरी क्लास को extend करती है। इस तरह के के inheritance का उपयोग basic programming में किया जाता है। 
      

MULTILEVEL INHERITANCE 

Multilevel inheritance में एक class दूसरी class को extend करती है और दूसरी class तीसरी class को extend करती है। 

HIERARCHICAL INHERITANCE 

Hierarchical inheritance में एक class को बहुत सी classes extend करती है। इस तरह  inheritance का प्रयोग जब किया जाता जब super class के task के कई sub task होते है। 
  

Inheriting a Class 

यदि आप किसी class को inherit करना चाहते है तो आप extends कीवर्ड उसे करते है। आप अपनी class के नाम के बाद extends keyword लगाते है और उसके बाद आप जिस class को inherit करना चाहते है उस class का नाम लिखते है। Inheritance का उदाहरण निचे दिया जा रहा है। 
class A
{
   
public void display()
  
{
     
System.out.println(“This is A class”);
  
}
 
 public static void main(String args[])
 
 {
 
      display();    
 
 }
}
class B
extends A
{
   
public static void main(String args[])
   
{
       
display();
   
}
}

Comments

Popular posts from this blog

C in Hindi : File Handling Introduction to C File Handling  एक file bytes की sequence होती है। File किसी भी data को store करने के लिए यूज़ की जाती है। Files को permanent storage के लिए यूज़ किया जाता है। जब आप किसी program को run करवाते है और उसका output देखने के बाद उसे बंद कर देते है तो उसका output वहीँ remove हो जाता है । यदि आप इस output को permanently store करना चाहते है तो C language के file handling feature को यूज़ कर सकते है। C file handling का यूज़ यँही ख़त्म नहीं होता है। यदि program में दिए जाने वाला input भी बहुत अधिक है तो किसी human द्वारा इसे enter कराने के बजाय आप file का यूज़ कर सकते है। इससे program automatically file से ही input ले लेगा। C language के द्वारा आप files से related ये काम कर सकते है। Existing files को open कर सकते है और नयी files create कर सकते है।  किसी भी program के output को file में store कर सकते है।  File के द्वारा किसी भी program में input भी दे सकते है।        सभी operatio...
Programming model of microprocessor 8085 prograaming modal  chhah saamaany prayojan rajistar 1.8085 mein chhah saamaany prayojan rajistar hote hain jaise b, ch, d, ai, h, l. 2. mote rajistar ko 16-bit opareshan (beesee, de, echel) karane ke lie rajistar jode ke roop mein joda ja sakata hai. sanchayakarta rajistar 1. yah akshar a dvaara pahachaana jaata hai. 2. yah alu ka ek hissa hai. 3. isamen 8-bit deta storej hai. 4. yah ankaganit aur taarkik sanchaalan karata hai. 5. kisee opareshan ka parinaam sanchaayak mein sangraheet kiya jaata hai. dhvaj panjee 1. yah bhee alu ka ek hissa hai 2.8085 mein paanch jhandon ke naam hain -jiro jhanda (z) -kaary dhvaj (chy) -sain phlaig (es) -parivartan dhvaj (p) sahaayak kairee phlaig (esee) 3. in jhande dhvaj rajistar mein paanch phlip-phlop hain. 4. ek ankaganit / tark sanchaalan ka nishpaadan in jhandon ko set ya reeset kar sakata hai. 5. sophtaveyar nirdeshon ke maadhyam se jhande (set ya reeset) ka pareeks...
Java in hindi : Introduction    History of Java  Java एक general purpose object oriented programming लैंग्वेज है। Sun Micro-systems ने java 1991 में U.S. में बनायीं थी। Java का नाम पहले oak रखा गया था लेकिन बाद में इसे change करके java किया गया था। Java James Gosling ने develop की थी। Java को consumer electronics जैसे की TV, VCR आदि software बनाने के लिए develop किया गया था।  Java का सबसे important और popular feature था की java platform independent थी। क्योंकि java किसी एक particular hardware या operating system के लिए नहीं बनायीं गयी थी। Java में बनाये हुए program किसी भी system पर execute किये जा सकते है। Java का ये feature java को आज भी सबसे popular लैंग्वेज बनाता है।  Java version  . ЈDK  Alpha and Beta (1995) .JDK 1.0 (23 Jan  , 1996) .  JDK 1.1(19 Fe, 1997) .   J2SE 1.2 (8 Dese, 1998)  .  J2SE 1.3 (8 May, 2000)  .  J2SE 1.4 (6 Feb, 2002)  .  J2SE 5.0 (30 Feb, 2...