Friday, November 21, 2014

How to implement an interface in a class but prohibit the class from implementing the interface methods in that class ?

Hi Friends,

Today I will present an interesting scenario in front of you . Normally we , most of the time will not come across such a scenario while coding . But there are exceptions and this happens to be ab exceptional exception that we should get to know.

Scenario :  Consider an interface K which has 2 methods in it : methA(), methB(). Now we need to have two classes A and B  in such a way that they will implement the interface K . But Class  A will only over ride methA() and Class B will implement methB() . How can we achieve that ?

Solution :

Normally when you implement an interface in a concrete class you will have to override the methods present in the interface otherwise the compiler will give an error . So  how we accomplish the  un accomplish-able like scenario stated above .

The answer is an Abstract Class . Yes , you heard it right : abstract class. An abstract class can implement an interface but it is not obliged to override any of the methods in the interface .

So for  our scenario , we can do it the following way :

public interface K {

public void methA();

public void methB();

}

Now we can  write our abstract class like this :

public abstract class A implements K{

}

You can check the above piece of code by running it in IDE of your choice (Eclipse, Netbeans etc). It will not throw any compiler error, because, an abstract class is not required  to override  any method in the interface , but it can if you want it to .

Hence, in this case, as we want to override methA() in class A, we can re organize Class A as :

public abstract class A implements K{

      @Override
      public void methA(){

          System.out.println("methA");

      }

}

Now we can use the class B as a concrete class and have it extend B, like below :

public  class B extends A{


}

If you write just the above piece of code , the compiler will throw error and ask you to implement the un-implemented method methB(). That's because B is a concrete class and it implements K via A. Since, methA() is already overidden in class A , so the compiler asks the developer to override and implement methB() in class B.

In light of the above explanation we can organize the class B as :

public  class B extends A{

      @Override
      public void methB(){

          System.out.println("methB");

      }

}

You can refer this stack overflow thread for more details : http://stackoverflow.com/questions/197893/why-an-abstract-class-implementing-an-interface-can-miss-the-declaration-impleme

That's it for today. Hopefully this will enlighten people who are not  aware of this concept .


3 comments:

  1. I know that people say there are always vulnerabilities, but what if there weren't. Or to be much more realistic; hard to find. What do hackers do then? If there is nothing for them to exploit how can they gain access to what ever it is that they are targeting? Is finding vulnerabilities then exploiting them the only way?

    For the record, I have no interest in unethical hacking. I just want to find other ways to protect my website and programs.
    open source cdn

    ReplyDelete
  2. I could not understand how hacking's related to the above post .

    ReplyDelete
  3. Had been thinking about it.... Could not think of a practical scenario. Do you have any? Tell me business case if possible.

    ReplyDelete