Skip to main content

Method Overriding in Java

Method Overriding in Java: किसी Super Class में किसी Method का जो Signature होता है, उसी Signature का Method जब हम Sub Class में Create करते हैं, तो इस प्रक्रिया को Method Overriding कहा जाता है। जबकि Super Class व Sub Class दोनों में ही Method का नाम समान हो, लेकिन उनके Signature में अन्तर हो, तो फिर चाहे ये Methods Super Class में हों चाहे Sub Class में ऐसे Methods Overloaded Methods कहलाते हैं और इस प्रक्रिया को Method Overloading कहा जाता है। Method Overloading के बारे में हम पिछले अध्‍याय में पढ चुके हैं। इस अध्‍याय में हम Method Overriding के बारे में जानेंगे।

Rules for method overriding


  • एक declared final method को override नहीं किया जा सकता।
  • Java में, एक method केवल sub-class में लिखी जा सकती है, class में नहीं।
  • तर्क सूची उस class की override method के समान होगी।
  • किसी भी तरीके को जो स्थिर है override करने के लिए उपयोग नहीं किया जा सकता है।
  • यदि किसी method को inheritance में नहीं लिया जा सकता है, तो इसे override नहीं किया जा सकता है।
  • Constructors override नहीं किए जा सकते है।
जावा method overriding का क्या उपयोग होता है आइये देखतें है −
  • Method overriding का उपयोग रन टाइम polymorphism के लिए किया जाता है।
  • Method overriding का उपयोग एक Method के specific implementation प्रदान करने के लिए किया जाता है जो पहले से ही इसके superclass द्वारा प्रदान किया जाता है।
// example: 
	class Employee
	{
		private int ID;	
		private String name;	
	
		public void setData(int empID, String empName)
		{
			ID = empID;
			name = empName;
		}
	
		public void showData()
		{
			System.out.println("\tName   : " + name);
			System.out.println("\tID     : " + ID);
		}
	}
	
	class Scientist extends Employee
	{
		private float salary;	
	
		public void setData(int empID, String empName, float sciSalary)
		{
			super.setData(empID, empName);
			salary = sciSalary;
		}
	
		public void showData()
		{
			super.showData();
			System.out.println("\tSalary : " + salary);
		}
	}
	
	class InheritanceDemo
	{
		public static void main(String args[])
		{
			Employee you = new Employee();
			Scientist me = new Scientist();
	
			you.setData(5000, "IDP");
			me.setData(7000, "Ajay", 20000);
			
			System.out.println("Your Information");
			you.showData();
	
			System.out.println("\nMy Information");
			me.showData();
		}

// Output 
   Your Information
        Name   : IDP
        ID     : 5000

   My Information
        Name   : Ajay
        ID     : 7000
        Salary : 20000.0

Comments

Popular posts from this blog

    METACHAR ACTER   UNIX  shell  अनेक मेटाचारेक्टर प्रोवाइड करता है जो किसी भी शैल स्कफ्रिप्त में उनका उसे करते टाइम विषेस माइनिंग रखते और जब तक काशी सोवळे नहीं हो जाते तब काट किसी वर्ड का एंडिंग का कारन बनते है।                                                     ex:--     एक directory में फिलो को लिस्टिंग करते टाइम एक सिंगल चैरेक्टर में रखा जाता है और * एक से अधिक वर्ड को मैचेस करता है यह शैल के अधिकांश पत्रों को लिस्ट दी गयी है जिन्हे metacharacter बोला  जाता है.   * ? [ ] " ' \    /    $ : ; ( ) | ^ < >  \.   नई लाइन स्पेस तब   #!/bin/sh  echo hello; word  #!/bin/sh  echo "I have \$1200" 1. <  single quotes : all special charecters between these quotes lose their special.  ex. --...
Until loop  The  until  loop is very similar to the  while  loop, except that the loop executes until the  TEST-COMMAND  executes successfully. As long as this command fails, the loop continues. The syntax is the same as for the  while  loop: until TEST-COMMAND; do CONSEQUENT-COMMANDS; done The return status is the exit status of the last command executed in the  CONSEQUENT-COMMANDS  list, or zero if none was executed. TEST-COMMAND  can, again, be any command that can exit with a success or failure status, and  CONSEQUENT-COMMANDS  can be any UNIX command, script or shell construct. Example :--            Write a shell script to display first ten positive numbers using until loop.                                     Program num=1 until [ $num –gt 10 ]; do echo num ...