Video 57

Yash
0

 


Program:-

interface MyCamera{
    void takeSnap();
    void recordVideo();
    private void greet(){
        //the method is declared private to avoid overriding it, and we can use it in the default methods to provide extra logic.
        System.out.println("Good morning");
    }
    default void record4kVideo(){
        //here the use of default method is that if you want a method in a interface to not be overridden and just want to give some logic in it we can use the default method .
        //you can also override a default method if you want there is no restriction on it.
        greet();
        System.out.println("Recording in 4k...");
    }
}

interface MyWifi{
    String[] getNetworks();
    void connectToNetwork(String network);
}

class MyCellPhone{
    void callNumber(int phoneNumber){
        System.out.println("Calling " + phoneNumber);
    }
    void pickCall(){
        System.out.println("Connecting...");
    }
}

class MySmartPhone extends MyCellPhone implements MyWifi, MyCamera{
    public void takeSnap(){
        System.out.println("Taking snap");
    }
    public void recordVideo(){
        System.out.println("Taking snap");
    }
// public void record4kVideo() {
// System.out.println("Taking snap and recording in 4k");
// }
    public String[] getNetworks(){
        System.out.println("Getting List of Networks");
        String[] networkList = {"Harry", "rohan", "Anjali5G"};
        return networkList;
    }
    public void connectToNetwork(String network){
        System.out.println("Connect to "+network);
    }
}

public class Default_method_V57 {
    public static void main(String[] args) {
        MySmartPhone ms = new MySmartPhone();
        ms.record4kVideo();
        //ms.greet();---> Throws an error!
        String[] ar = ms.getNetworks();
        for(String item:ar){
            System.out.println(item);
        }
    }
}

Post a Comment

0 Comments
Post a Comment (0)
To Top