Can a normal Class implement multiple interfaces?

后端 未结 8 1077
春和景丽
春和景丽 2021-01-31 01:34

I know that multiple inheritances between Interfaces is possible, e.g.:

public interface C extends A,B {...} //Where A, B and C are Interfaces

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 02:29

    Yes, a class can implement multiple interfaces. Each interface provides contract for some sort of behavior. I am attaching a detailed class diagram and shell interfaces and classes.

    Ceremonial example:

    public interface Mammal {
        void move();
        boolean possessIntelligence();
    }
    
    public interface Animal extends Mammal {
        void liveInJungle();
    }
    
    
    public interface Human extends Mammal, TwoLeggedMammal, Omnivore, Hunter {
        void liveInCivilization();
    }
    
    public interface Carnivore {
        void eatMeat();
    }
    
    public interface Herbivore {
        void eatPlant();
    }
    
    public interface Omnivore extends Carnivore, Herbivore {
        void eatBothMeatAndPlant();
    }
    
    public interface FourLeggedMammal {
        void moveWithFourLegs();
    }
    
    public interface TwoLeggedMammal {
        void moveWithTwoLegs();
    }
    
    public interface Hunter {
        void huntForFood();
    }
    
    public class Kangaroo implements Animal, Herbivore, TwoLeggedMammal {
        @Override
        public void liveInJungle() {
            System.out.println("I live in Outback country");
        }
    
        @Override
        public void move() {
            moveWithTwoLegs();
        }
    
        @Override
        public void moveWithTwoLegs() {
            System.out.println("I like to jump");
        }
    
        @Override
        public void eat() {
            eatPlant();
        }
    
        @Override
        public void eatPlant() {
            System.out.println("I like this grass");
        }
    
        @Override
        public boolean possessIntelligence() {
            return false;
        }
    }
    
    
    public class Lion implements Animal, FourLeggedMammal, Hunter, Carnivore {
        @Override
        public void liveInJungle() {
            System.out.println("I am king of the jungle!");
    
        }
    
        @Override
        public void move() {
            moveWithFourLegs();
        }
    
        @Override
        public void moveWithFourLegs() {
            System.out.println("I like to run sometimes.");
        }
    
        @Override
        public void eat() {
            eatMeat();
        }
    
        @Override
        public void eatMeat() {
            System.out.println("I like deer meat");
        }
    
        @Override
        public boolean possessIntelligence() {
            return false;
        }
    
        @Override
        public void huntForFood() {
            System.out.println("My females hunt often");
        }
    }
    
    public class Teacher implements Human {
        @Override
        public void liveInCivilization() {
            System.out.println("I live in an apartment");
        }
    
        @Override
        public void moveWithTwoLegs() {
            System.out.println("I wear shoes and walk with two legs one in front of the other");
        }
    
        @Override
        public void move() {
            moveWithTwoLegs();
        }
    
        @Override
        public boolean possessIntelligence() {
            return true;
        }
    
        @Override
        public void huntForFood() {
            System.out.println("My ancestors used to but now I mostly rely on cattle");
        }
    
        @Override
        public void eat() {
            eatBothMeatAndPlant();
        }
    
        @Override
        public void eatBothMeatAndPlant() {
            eatPlant();
            eatMeat();
        }
    
        @Override
        public void eatMeat() {
            System.out.println("I like this bacon");
        }
    
        @Override
        public void eatPlant() {
            System.out.println("I like this broccoli");
        }
    }
    

提交回复
热议问题