Interface changes in Java 8 and 9 in Hindi

Last Updated on October 23, 2022 by RAJENDRAPRASAD

Interface changes in Java 8 and 9 in Hindi – Hello दोस्तों rajhindime.in में आपका स्वागत है |

दोस्तों, पिछले post में आपने OOPs concept के Interface के बारे में विस्तार से जाना |

आज के इस पोस्ट Interface changes in Java 8 and 9 in Hindi में आप Interface के अन्य features के बारे में विस्तार से जानेंगे जिन्हें, Java 8 और 9 में add किया गया |

Interface before Java 8

अब तक (Java 8 के पहले) आपने जाना कि, interface में केवल constant variables और abstract methods ही होते हैं, जब भी कोई Class उस Interface को implements करता है तब, उसे उस Interface के सारे methods को override करके implementation body देना होता है | यदि कोई Class, Interface के किसी भी method को implementation body नहीं देता तब, उस स्थिति में उस Class को भी abstract declare करना होता है |

इस तरह java 8 के पहले interface में 2 ही features हैं :

1. Constant variable

2. Abstract method

Program 1:

interface Vehicle {
 
    void move();
}
 
class Car implements Vehicle {
 
    @Override
    public void move() {
        System.out.println("moving car with the help of Steering wheel");
    }
}
 
class AutoRickshaw implements Vehicle {
 
    @Override
    public void move() {
        System.out.println("moving AutoRickshaw with the help of handles");
    }
}
 
class InterfaceDemo2 {
    public static void main(String args[]) {
        Vehicle v;
        v = new Car();
        v.move();
        v = new AutoRickshaw();
        v.move();
    }
}

OutPut:

moving car with the help of Steering wheel
moving AutoRickshaw with the help of handle

Explanation:

ऊपर program 1 में हमनें Interface Vehicle में move() method declare किया |

दोनों Class Car और AutoRickshaw ने , Interface Vehicle को implements करके उसके method move() को अपने – अपने class में override करके उसकी implementation body {} define की |

जैसा कि आप जानते ही हैं, “आवश्यकता ही अविष्कार की जननी है” |

समय के अनुसार java में जरुरत के हिसाब से कुछ नए features को add किया जाता रहा है |

Problem in interface before java 8:

मान लीजिए, हमे interface में कुछ नए methods को add करना है परन्तु, अगर हम ऐसा करते  हैं ,तब किसी भी application में, जिस-जिस class ने उस interface को implement किया होगा, उन्हें उस नए method को भी override करके उसे implementation body देना ही होगा, और अगर सारे class ऐसा नहीं करते तब, हमें उस class में compile time error मिलेगा और हमारा application run नहीं होगा |

बात अगर एक-दो class की हो, तब हम उस method को override करके उसे implementation body दे भी सकते हैं परन्तु, कल्पना कीजिए कि कोई बहुत बड़ा  application है, तब उस case में तो सारे class में उस method को override करना बहुत ही मुश्किल होगा |

Interface new feature in java 8:

ऊपर बताए गए कमी को पूरा करने के लिए, Java 8 interface में दो नए methods default और static को add किया गया |

अब java 8 से interface में 4 features हैं :

1. constant variables

2. abstract methods

3. default methods

4. static methods

Java interface default method:

Java 8 से हम interface में default method create कर सकते हैं, जिसमे implementation body {} देना जरुरी है |

Default method को define करने के लिए default keyword का use किया जाता है |

private और abstract keyword कभी भी साथ में use नहीं कर सकते |

Syntax:

interface interfaceName {
	default return_type methodName() {
		// method logic
	}
}

Example:

interface InterfaceDemo1 {

	default void show() {
		System.out.println("this is default method show");
	}
}

NOTE:

1. default method में implementation body होना ही चाहिए, ऐसा न करने पर, compile time error मिलेगा |

2. एक interface के अंदर multiple default methods हो सकते हैं |

3. implementation class को default methods को override करना जरुरी नहीं है परन्तु,  आवश्यकता पड़ने पर override भी कर सकते हैं |

4. यहाँ default method, protected access modifier की तरह कार्य करता है |

चूँकि, default method को override करना जरुरी नहीं है, इस तरह हमारी problem जो java 8 interface के पहले थी वो, solve हो गई |

हमें कभी-कभी default method को भी override करना पड़ता है, आइए program द्वारा समझें |

जैसा कि आप जानते ही हो कि java में multiple inheritance allowed नहीं है, क्योंकि   इससे Diamond problem उत्पन्न होता है, जहाँ compiler यह नहीं decide कर पाता कि किस super class के method को use करे |

ठीक उसी तरह default method के कारण interface में भी यह problem उत्पन्न होता है |

अगर कोई class दो या दो से अधिक ऐसे interfaces को implement करता है, जिसमें default method के name common/same हैं | ऐसी स्थिति में भी compiler नहीं decide कर पता कि किस interface के default method को execute करे | इसलिए यहाँ default method को override करना जरुरी हो जाता है, और अगर हम ऐसा नहीं करते तो compile time error मिलता है |

Program 2:

interface Interface1 {
	
	abstract void reveal();

	default void show() {  //common method
		System.out.println("this is default method show from interface1");
	}

	default void display() {
		System.out.println("this is default method display from interface1");
	}
}
//-------------------------------------------------
interface Interface2 {

	default void show() {  //common method
		System.out.println("this is default method show from interface2");
	}

	default void expose() {
		System.out.println("this is default method expose from interface2");
	}
	
	abstract void reveal();
}
//---------------------------------------------------
public class MainClass implements Interface1, Interface2 {

	@Override
	public void reveal() {

		System.out.println("this is implemented method from interface1");
	}

	@Override
	public void show() { // overriding common method from both interface
		System.out.println("overridden method show to solve Diamond Problem");
	}

	public static void main(String[] args) {
		MainClass mc = new MainClass();
		mc.reveal();
		mc.show();
		mc.display();
		mc.expose();
	}
}

OutPut:

this is implemented method from interface1
overridden method show to solve Diamond Problem
this is default method display from interface1
this is default method expose from interface2

Explanation:

ऊपर के program 2 में, 2 interfaces हैं Interface1 और Interface2 |

Interface1 में, एक abstract method reveal() तथा 2 default methods show() और display() हैं |

Interface2 में, 2 default methods show() तथा expose() हैं |

NOTE: default method show() यह दोनों ही interface में common method है |

Class MainClass यह Interface1 तथा Interface2 दोनों को implements कर रहा है |

यहाँ हमने method reveal() को override करके उसे implementation body दिया है, जो कि Interface1 में abstract method है |

Method show() को भी override किया है (जो दोनों interface में common है), यदि हम ऐसा नहीं करते तो Diamond problem उत्पन्न होने के कारण हमें compile time error मिलेगा |

NOTE:

अगर interface/class के किसी भी method को उसके implementation/child class के अंदर override किया गया है तब, run time में हमेशा वही overridden method ही call होगा |

Program 3:

interface Interface2 {

	default void show() {  
		System.out.println("this is default method show from interface2");
	}

	default void expose() {
		System.out.println("this is default method expose from interface2");
		show();
	}
	
	abstract void reveal();
}
//---------------------------------------------
public class MainClass implements Interface2 {

	@Override
	public void reveal() {

		System.out.println("This is implemented method");
	}
	
	@Override
	public void show() {
		System.out.println("overridden method show");
	}

	public static void main(String[] args) {
		MainClass mc = new MainClass();
		mc.expose();
	}
}

OutPut:

this is default method expose from interface2
overridden method show

Explanation:

ऊपर program 3 में, interface2 में show() method को internally expose() method में call किया गया है | MainClass में show() method को override किया गया है |

इसलिए run time में show() method का overridden body “overridden method show” print हुआ है न कि “this is default method show from interface2

Java interface static method:

Interface static method, default method की तरह ही होता है, बस फर्क इतना है कि हम static method को implementation class में override नहीं कर सकते |

NOTE:

1. static method में implementation body होना ही चाहिए, ऐसा न करने पर, compile time error मिलेगा |

static और abstract keyword कभी भी साथ में use नहीं कर सकते |

2. एक interface के अन्दर multiple static methods हो सकते हैं |

3. interface static method को हमेशा, Interface name का use करके ही call करना होता है, वरना compile time error मिलेगा |

4. static method, यह implementation class में होने वाले अनचाहे override को रोकता है |

Program 4:

interface Interface1 {

	abstract void reveal();

	default void show() {
		System.out.println("This is default method show");
	}

	static void display() {
		System.out.println("This is static method display");
	}
}
public class MainClass implements Interface1 {

	@Override
	public void reveal() {

		System.out.println("This is implemented method");
	}

	public static void main(String[] args) {
		MainClass mc = new MainClass();
		mc.reveal();
		mc.show();
//		mc.display(); //will give compile time error
		Interface1.display();
	}
}

OutPut:

This is implemented method
This is default method show
This is static method displ

Explanation:

ऊपर program 4 में, method display() यह एक static method है, जिसे class MainClass में interface name का use करके call किया गया है |

Problem with default and static method:

मान लो, हमने जितने भी static और default methods create किए हैं, उसमें बहुत सारे code common हैं | इससे हमारे overall applications में lines of code बढ़ जाते हैं, जो कि अच्छा नहीं है |

कितना अच्छा हो कि हम उन बार-बार use होने वाले code को use करके कोई method बना ले और उन्हें अपने default अथवा static method में call कर ले, जिससे code duplication कम हो जाए और lines of code भी कम हो जाए |

Interface new features in java 9:

ऊपर बताए गए कमी को पूरा करने के लिए ही java 9 में private और private static method को introduce किया गया है |

अब java 9 से interface में 6 features हैं :

1. constant variables

2. abstract methods

3. default methods

4. static methods

5. private method

6. private static method

Private and private static method in java interface:

Java 9 से, हम interface me private तथा private static method create कर सकते हैं, जिसमें implementation body देना जरुरी है |

private method को define करने के लिए private keyword का use किया जाता है |

Syntax:

interface interfaceName {
	private return_type methodName() {
		// method logic
	}
	private static return_type methodName() {
		// method logic
	}
}

Example:

interface InterfaceDemo1 {

	private void show() {
		System.out.println("this is private method show");
	}
	private static void display() {
		System.out.println("this is private static method display");
	}
}

NOTE:

1. private तथा private static methods में implementation body होना ही चाहिए, ऐसा न करने पर, compile time error मिलेगा |

2. एक interface के अंदर multiple private तथा private static method हो सकते हैं |

3. private तथा private static methods केवल interface के अंदर ही accessible होते हैं, अर्थात इन्हें interface के बाहर access नहीं किया जा सकता |

4. यदि एक interface दूसरे किसी interface को extends करता है तो, उस स्थिति में भी private method inherit नहीं होते हैं |

5. चूँकि दोनों ही methods private हैं इसलिए, इन्हें implementation class में access /call नहीं कर सकते

6. इन दोनों methods का एक मात्र उद्देश्य (purpose) है, code की reusability को बढ़ाना | यह दोनों methods, interface में defined दूसरे अन्य method के अन्दर call किए जाते हैं |

7. non-static methods को किसी भी static method में call नहीं किया जा सकता | Static method के bare में अधिक जानने के लिए method in java in Hindi को पढ़ें |

Program 5:

interface Interface1 {

	abstract void reveal();

	default void show() {
		System.out.println("This is default method show");
		expose(); // calling private method
	}

	static void display() {
		System.out.println("This is static method display");
//		show();   // will give compile time error as non-static method can not be called inside static method
		demonstrate(); // calling private static method inside static method
	}

	private void expose() {
		System.out.println("This is private method expose");
	}

	private static void demonstrate() {
		System.out.println("This is private static method demonstrate");
//		expose(); // will give compile time error as non-static can not be called inside static
	}
}
//---------------------------------
public class MainClass implements Interface1 {

	@Override
	public void reveal() {
		System.out.println("Implementing abstract method reveal from interface1");
	}

	public static void main(String[] args) {
		Interface1 interface1 = new MainClass();
		interface1.reveal();
		interface1.show();
//			interface1.display(); // give error as static method of interface can not be called using reference variable
		Interface1.display(); // calling static method using Interface name
//			interface1.expose(); //give error as can not be called as private method
//			interface1.demonstrate(); //give error as can not be called as private method
	}
}

OutPut:

Implementing abstract method reveal from interface1
This is default method show
This is private method expose
This is static method display
This is private static method demonstrate

Explanation:

ऊपर program 5 में, हमने interface के सभी methods को define किया है, तथा methods को एक-दूसरे के अंदर call किया है |

NOTE:

चूँकि java 8 से हम interface में static method define कर सकते हैं, इसलिए हम interface के अंदर Main() method भी लिख सकते है जो कि वैसा ही behave करेगा जैसे कि किसी class में करता है |

Program 6:

interface Interface1 {

	abstract void reveal();

	default void show() {
		System.out.println("This is default method show");
	}

	static void display() {
		System.out.println("This is static method display");
	}

	private void expose() {
		System.out.println("This is private method expose");
	}

	private static void demonstrate() {
		System.out.println("This is private static method demonstrate");
	}

	public static void main(String[] args) {
		Interface1.display(); // calling static method using Interface name
		Interface1.demonstrate();
	}
}

OutPut:

This is static method display
This is private static method demonstrate

Explanation:

ऊपर program 6 में, हमने Main() method को Interface1 के अंदर ही define किया है |

NOTE:

1. interface के Main method में हम केवल static method को ही call कर सकते हैं |

2. चूँकि interface का object नहीं create किया जा सकता, इसलिए non-static variable को interface के Main method में call नहीं कर सकते |

Conclusion – आज आपने क्या सीखा

इस post में आपने जाना कि java 8 से हम interface के अंदर default तथा static method और java 9 से private तथा private static methods create कर सकते हैं |

साथ ही साथ यह भी जाना कि, हम java 8 से interface के अंदर Main() method को भी create कर सकते हैं |

आशा है कि, आपको मेरा यह Blog Interface changes in Java 8 and 9 in hindi, जरूर पसंद आया होगा |

अगर आप इस post से related कोई सवाल पूँछना चाहते हैं अथवा कोई सुझाव देना चाहते हैं तो comment करके जरूर बताएं, मैं उसका reply जरूर दूँगा |

इस post को अपना कीमती समय देने के लिए बहुत बहुत धन्यवाद् | फिर मिलेंगें |

Leave a Comment