Is lesson me hum seekhenge:
- Interface kya hota hai
- Interface me method declaration
- Subclass me method body implement karna
- implements keyword
- extends keyword
- Interface aur Abstract Class ka relation
- Interface + Abstract Class example
Interface ek fully abstract blueprint hota hai.
Iska use hota hai:
100% abstraction achieve karne ke liye
Interface me normally hota hai:
method declaration
constants
Interface me methods by default:
public abstract
interface Animal {
void sound();
}Ye actually internally hota hai:
interface Animal {
public abstract void sound();
}Interface me method sirf declare hota hai, body nahi hoti.
Example
interface Vehicle {
void start();
void stop();
}Yahan:
start()
stop()
sirf declare hue hain.
Interface ko implement karne wali class me methods ki body likhni padti hai.
Example
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound(){
System.out.println("Dog barks");
}
}
public class Test {
public static void main(String[] args){
Dog d = new Dog();
d.sound();
}
}Output
Dog barks
implements keyword use hota hai:
interface ko implement karne ke liye
Example
class Dog implements AnimalMatlab:
Dog class Animal interface ke methods implement karegi
Interface bhi dusre interface ko extend kar sakta hai.
Example
interface Animal {
void eat();
}
interface Dog extends Animal {
void bark();
}Ab Dog interface me:
eat()
bark()
dono methods honge.
Abstract class aur interface saath me use ho sakte hain.
Example
Interface → method rules define karta hai
Abstract class → partial implementation deta hai
Subclass → final implementation deta hai
Example
interface Vehicle {
void start();
}
abstract class Car implements Vehicle {
void fuelType(){
System.out.println("Petrol Car");
}
}
class BMW extends Car {
public void start(){
System.out.println("BMW Starting");
}
}
public class Test {
public static void main(String[] args){
BMW car = new BMW();
car.start();
car.fuelType();
}
}Output
BMW Starting
Petrol Car
Explanation:
Interface → start() declare
Abstract Class → fuelType() implement
Subclass → start() implement
✔ Interface ka object directly nahi bana sakte
❌
Vehicle v = new Vehicle(); // error
✔ Interface methods by default:
public abstract
✔ Interface variables by default:
public static final
Example
interface Test {
int number = 10;
}General structure:
interface InterfaceName {
method declaration
}
abstract class Parent implements InterfaceName {
}
class Child extends Parent {
method implementation
}
1️⃣ Interface kya hota hai?
2️⃣ Interface aur abstract class me difference kya hai?
3️⃣ implements keyword ka use kya hai?
4️⃣ Interface method body kaha likhte hain?
5️⃣ Interface interface ko kaise extend karta hai?
Is lesson me humne seekha:
✔ Interface concept
✔ Interface method declaration
✔ Subclass me method implementation
✔ implements keyword
✔ extends keyword
✔ Interface + Abstract class relation
Interface Java me loose coupling aur abstraction achieve karne ke liye bahut important concept hai.