Welcome To Tips Arena
Hello dosto aapka ek bar firse swagat hai humare Tips Arena blog par dosto humne apni peechli post main seekha tha Java Exception Handling ke bare main or dosto ab hum seekhenge Java Interfaces ke bare main. To dosto aayiye seedhe chalte hain apne topic par.Java Interfaces Full Guide In Hindi
Friends hum Java Interface ko ek example ki help se seekhne ka prayas karte hain, friends for example just think aapka ek friend hai or aapke friend ki ek company hai or friends aap bhi ek company open karna chahte hain, so friends ab aap apne friend se puchhte hain ki mujhe company open karne ke liye kya karna hoga ? aapka vo friend aapko batata hai ki sales department, management department, accounts department, production department aapko setup karna hoga etc etc... friends aapke friend ke dwara diye gaye suggestions ke baad ye aap par depend karta hai ki aapko kya karna hai. to friends jese aapke friend ne aapko ek company open karne ke liye kuch suggestions diye dosto thik usi trah interfaces classes ko batate hain ki unhe kya karna hai. vo class kaise work karti hai ye us class par depend karta hai.
Friends Java interface thik usi trah hai jaise ki hum exam time main sirf topics ki headings ko yaad karte hain or exam main un headings ki detail apne mind se likh kar aate hain, Dosto interface kisi boss ki trah hota hai or class kisi employee ki trah hoti hai. Interface class ko kaam batata hai or class us kaam ko karti hai. Interface koi bhi kaam khud nahi karta hai. Interface main define kiye gaye methods ko hume kisi task ki trah dekhna chahiye jo interface class se karwana chahta hai.
Interfaces main define gye sabhi methods by default abstract hote hain. means inki koi body nahi hoti hai. Interface ko hamesa public declare kiya jata hai. jab koi class interface ko implement karti hai to use interface main declare kiye gye sabhi methods ki definition provide karni hoti hai. hum kisi method ko chhod nhi sakte hain. ek interface ko koi class extend nhi karti hai. Interfaces hamesha implement kiye jate hain, iske liye hum implements keyword use karte hain.
Friends Java main multiple inheritance allow nhi hain. lekin java main interfaces ke through multiple inheritance implement kiya ja sakta hai. Interfaces super class ki trah mehods provide karte hain, bas unki definitions hume implement karne wali class main deni hoti hai. Interfaces classes ki trah hoti hain, lekin hum inka object nhi create kar sakte hain or kisi method ki body bhi hum nahi declare kar sakte hain.
Methods ke saath hi hum interfaces main variables bhi declare kar sakte hain. Interfaces main declare kiye jane wale variables static or final hone chahiye. Dosto ek interface ek se jada interfaces ko extend kar sakta hai ek bar interface declare karne ke baad kitni bhi classes us interface ko implement kar sakti hain. or ek class jitne chahe utne interface implement kar sakti hai. iske liye hume sabhi interfaces ke names ko (,) se separate karna hoga.
Interfaces classes ke saath Is-a relationship establish karte hain. for example humare paas ek interface hai vegetables ke name se jisme color or taste etc methods hain or ek Tomato name ki class hai. friends ab agar tomato class vegetable interface ko implement karti hai to inme Is-a relation hoga. yaani ki hum bol sakte hain Tomato is a vegetable.
Working with Interfaces
Friends Interfaces ko declare karna bilkul easy hota hai. dosto iske liye hum interface keyword use karte hain. pehle to hum public access modifier define karte hain, iske baad hum interface keyword likh kar interface ka name likhte hain. iske baad curly brackets main hum un sabhi methods ko declare karte hain jo hum kisi class se implement karwana chahte hain. wese to interfaces ke under sabhi methods by default public hote hain. lekin hum chahe to public access modifier sabhi methods ke aage add kar sakte hain. See Example Below:-
Structure
|
Example
|
public interface interface_Name
{
// methods without body;
}
|
public interface yrFriend
{
public void sales();
public void
production();
public void accounts();
public void
management();
}
|
Friends jaise ki humne aapko bataya tha ki implements keyword ko use karte huye koi bhi class easily interface ko implement kar sakti hai. Interfaces ka complete example hum aapko neeche bata rahe hain :-
Example
public interface yourFriend
{
public void sales();
public void production();
public void accounts();
public void management();
}
public class You implements yourFriend
{
public void sales()
{
// implement sales department
}
public void production()
{
// implement production department
}
public void accounts()
{
// implement production department
}
public void management()
{
// implement production department
}
}
No comments:
Post a Comment