not implementing all of the methods of interface. is it possible?

前端 未结 10 1954
挽巷
挽巷 2020-11-28 03:33

Is there any way to NOT implement all of the methods of an interface in an inheriting class?

10条回答
  •  再見小時候
    2020-11-28 04:31

    We can override all the interface methods in abstract parent class and in child class override those methods only which is required by that particular child class.

    Interface

    public interface MyInterface{
        void method1();
        void method2();
        void method3();
    }
    

    Abstract Parent class

    public abstract class Parent implements MyInterface{
    @Override
    public void method1(){
    
    }
    @Override
    public void method2(){
    
    }
    @Override
    public void method3(){
    
    }
    }
    

    In your Child classes

    public class Child1 extends Parent{
        @Override
        public void method1(){
    
        }
    }
    
    
    
    
    public class Child2 extends Parent{
        @Override
        public void method2(){
    
        }
    }
    

提交回复
热议问题