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. --...
UNIX in Hindi UNIX एक multi tasking, multi user operating system  है जिसे सन 1969  में AT & T LABS  में बनाया गया था। जिसे AT & T में काम करने वाले computer  scientist  ken Thompson  &   Denis Ritchie and  उनके friends ने मिलकर  बनाया गया था.  ऐसे AT & T   ने अपने उसे के लिए बनाया गया था लेकिन बाद में सन 1970 में इसे commercialize कर दिया गया था।  इस Operating System को ख़ास तौर पर Programmers और developers के लिए बनाया गया था। इसकी बजह यह थी की यह Modular Programming Interface design provide करता था जिसे "Unix Philosophy" भी कहा जाता है। UNIX Operating System को 'C' और Assembly Programming Languages में लिखा गया था। C Programming language को खास तौर पर UNIX बनाने के लिए ही develop किया गया था और इसे भी Denis Ritchie द्वारा ही बनाया गया था। आज भी 'C' Programming Language को बहुत ख़ास माना जाता है क्यूंकि Device Drivers इसी language में ज्यादातर लिखे जाते हैं और साथ ही इसे  Mother...
Addressing modes of microprocessor 8085 The way of specifying data to be operated by an instruction is called addressing Types of addressing modes – In 8085 microprocessor there are 5 types of addressing modes: Immediate Addressing Mode – In immediate addressing mode the source operand is always data. If the data is 8-bit, then the instruction will be of 2 bytes, if the data is of 16-bit then the instruction will be of 3 bytes. Examples: MVI B 45 (move the data 45H immediately to register B) LXI H 3050 (load the H-L pair with the operand 3050H immediately) JMP address (jump to the operand address immediately). Register Addressing Mode – In register addressing mode, the data to be operated is available inside the register(s) and register(s) is(are) operands. Therefore the operation is performed within various registers of the microprocessor. Examples: MOV A, B (move the contents of register B to register A) ADD B (add contents of registers A and B and s...