Monday, May 27, 2013

Can we create object to Interface and Abstract class ..?


Yes...(but not directly)

We can create the object to An Interface / Abstract class.

As per the java specification we are not allowed to create object to interface and abstract class directly. But we can do that indirectly with the help of anonymous inner type.

Interface:

An interface is one which contains only abstract methods( methods don't have body).
Any variable declared inside an interface are by default public static final and should be initialised

Syntax:



Abstract Class:

An Abstract class is a class that can contain abstract methods or concrete methods( methods those have body) or both.

Syntax:



What happens when we try to create object:

We have an interface SampleInt which contains an abstract method.




SampleAbs is the abstract class with a concrete method showSampleAbs().




Now we are trying to create object to the above defined interface and abstract class in the class App




So , as the compiler is not allowing us to create the object to interface and abstract class we can say We can't create object to an interface or abstract class directly .


How to create object to interface and abstract class:

We can create the object to interface or abstract class indirectly by using anonymous inner type.

App class shows creating object to SampleInt and SampleAbs.




By providing anonymous inner class to Abstract class or interface we can say they have intantiated indirectly.

We can call showSampleAbs() by using SampleAbs object sampleAbs aswellas we can call sampleIntMethod() defined in interface by using it's instance sampleInt.


Calling methods defined inside anonymous type:


Suppose we have defined some more methods inside the implementation (anonymous class), then we can't call them by using instance since the methods not available at the time of object creation.




Calling the methods that are defined inside anonymous inner type will be as shown. 




Note:

Anonymous inner type is simply providing implementation to the corresponding abstract class / interface. 

So we are able to create object to abstract class and an interface by providing implementation in the form of anonymous inner type.

You can observe the behaviour by practising this code.



42 comments:

  1. public interface Interface {
    public void m();
    }

    public class ImplementedClass implements Interface{

    public void m() {
    // TODO Auto-generated method stub

    }
    public static void main(String[] args) {
    Interface i = new ImplementedClass();
    i.m();
    i.toString();
    i.hashCode();
    Interface ii = new Interface()
    {
    public void m()
    {

    }
    public void m1()
    {

    }
    }.m1();
    System.out.println(i);
    System.out.println(ii);
    }

    }

    I am unable to call like AnnonymusClss(){public void newmethod(){}}.newmethod();

    ReplyDelete
    Replies
    1. Hi Sekhar,

      As i described in the article, you should call a method available as part of anonymous class with the help of the object directly(if it is already part of that interface), we should not use reference there as the method return type is void and you are equating it to Interface reference which is declined.

      here we go,

      public class ImplementedClass implements Interface{

      public void m() {
      // TODO Auto-generated method stub

      }
      public static void main(String[] args) {
      Interface i = new ImplementedClass();
      i.m();
      i.toString();
      i.hashCode();

      //should not use reference as method m1() is newly //defined in your anonymous class and is not there in the actual interface.
      new Interface()
      {
      public void m()
      {

      }
      public void m1()
      {

      }
      }.m1();
      System.out.println(i);
      System.out.println(ii);
      }

      }


      If m1() is also available as part of your Interface then you can approach in your previous way

      like

      Interface ii = new Interface()
      {
      public void m()
      {

      }
      public void m1()
      {

      }
      }.m1();

      I think u have understood the reason , if not feel free to revert back.

      And a suggestion, Avoid usage of keywords as your class/interface names. You should follow the naming conventions given by Sun ..

      Delete
  2. Nice blog.Screenshots helped a lot. ..thanks

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi Yes Pratap
    First thanks for sharing beautiful article.
    Here i have one question in my mind is like
    Can you please explain me what is actual use of annonymous class in java and in which situation we required to implement that?

    ReplyDelete
  5. Brother , spelling of developer is wrong in your "About Me" , thought you should correct it.

    ReplyDelete
  6. Hi Winner,
    it's preferred to use inner classes if we are using that piece of code in only one place. Writing a class is not required in this case. So directly we are implementing the interface/abstract class with help of anonymous class.

    For more information refer https://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html

    ReplyDelete

  7. After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
    Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in



    selenium training in Bangalore
    selenium training in Marathahalli
    selenium training in Btm layout
    selenium training in Jaya nagar
    selenium training in Electronic city
    selenium training in Kalyan nagar



    ReplyDelete
  8. Resources like the one you mentioned here will be very useful to me ! I will post a link to this page on my blog. I am sure my visitors will find that very useful
    Python Online training
    python Course institute in Chennai
    Python Course institute in Bangalore

    ReplyDelete
  9. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    Python Online training
    python Course institute in Chennai
    Python Course institute in Bangalore

    ReplyDelete
  10. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage
    contribution from other ones on this subject while our own child is truly discovering a great deal.
    Java training in Chennai

    Java Online training in Chennai

    Java Course in Chennai

    Best JAVA Training Institutes in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Training

    Java Online Training

    ReplyDelete
  11. Thanks mate. I am really impressed with your writing talents and also with the layout on your weblog. Appreciate, Is this a paid subject matter or did you customize it yourself? Either way keep up the nice quality writing, it is rare to peer a nice weblog like this one nowadays. Thank you, check also event marketing and How Blockchain Technology Can Influence the Event Industry

    ReplyDelete
  12. Great and I have a neat proposal: What House Renovations Need Council Approval top home renovation companies

    ReplyDelete