tampurus

7 Multiple inheritance using Interface

Apr 20th, 2022 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. // inheritance using interface
  2. interface base{
  3.     int x = 22;
  4. }
  5. class Main implements base{
  6.     public static void main(String args[]){
  7.         Main obj = new Main();
  8.         System.out.println(obj.x);
  9.     }
  10. }
  11. // output 22
  12.  
  13. // multiple inheritance using interface
  14. interface AnimalEat {
  15.    void eat();
  16. }
  17. interface AnimalTravel {
  18.    void travel();
  19. }
  20. class Animal implements AnimalEat, AnimalTravel {
  21.    public void eat() {
  22.       System.out.println("Animal is eating");
  23.    }
  24.    public void travel() {
  25.       System.out.println("Animal is travelling");
  26.    }
  27. }
  28. public class Demo {
  29.    public static void main(String args[]) {
  30.       Animal a = new Animal();
  31.       a.eat();
  32.       a.travel();
  33.    }
  34. }
  35. /*
  36. Output
  37. Animal is eating
  38. */
Add Comment
Please, Sign In to add comment