i don't want to define all the methods from an implemented interface,Perhaps i want to define one but all  as per my requirement!!!How to achieve this scenario in JAVA?

Comments

  1. Below is an example for this scenario:

    Here we r not defining the method m2() in class Ex2 bcoz we made the class as abstract ,So we don't need to define all the methods inplace its mandatory to define all the methods from the implemented interface in the extended class.
    e.g.:-

    interface A
    {
    public void m1();
    public void m2();
    }

    public abstract class Ex2 implements A
    {

    public static void main(String[] args)
    {
    // TODO Auto-generated method stub

    }
    public void m1(){
    System.out.println("Ex2 m1");
    }

    }
    class Test extends Ex2
    {
    public void m1()
    {
    System.out.println("Test m1");
    }
    public void m2()
    {
    System.out.println("Test m2");
    }

    }

    ReplyDelete
  2. Basically we have to define all the methods for an implemented interface only in the case of non-abstract classes but incase of abstract classes we may or may not define the methods

    ReplyDelete

Post a Comment