Results 1 to 6 of 6
  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default implementation of interfaces

    package interfce;

    interface A
    {
    Object x();
    }
    public class Mgr implements A
    {
    public static void main(String[] args)
    {
    A aa = new A()
    {
    public Object x()
    {
    return new Mgr();
    }
    };
    System.out.println(aa.x());
    }
    }

    though i implemented x() method in Mgr, i got an error stating that Mgr class has to implement an inherited abstract method x()


    can any one help me out y it is showing an error

    Thanks in advance
    Daya
    Last edited by Dayanand; 03-09-2011 at 09:20 AM. Reason: typing mistake

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    You don't realization a method 'x' in class Mrg. You just said that Mrg implement some interface. You need realizations all methods in interface.
    for example
    Java Code:
    interface OneArg<A> {
        void print(A a);
        A operation(A a, A b);
    }
    
    public class ScEx implements OneArg<String> {
    
         public static void main(String[] arg) throws Exception {
             ScEx scEx = new ScEx();
             String value = scEx.operation("Hello", "world");
             scEx.print(value);
    
         }
    
        public void print(String s) {
            System.out.println(s);
        }
    
        public String operation(String s, String b) {
            return s + " " + b;
        }
    }
    Last edited by Petr; 03-09-2011 at 09:25 AM.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You also designed the method in the main method. You need to do it outside of the main method
    Java Code:
    interface A{
      void objectX();
    }
    
    public class Mgr implements A{
      public void objectX(){
        System.out.println("ObjectX");
      }
      public static void main(String[] args){
        Mgr m = new Mgr();
        m.objectX();
      }
    }
    Is how the code should generally be designed.

    If a class implements an interface the class must have each method declared in the interface. main should be a separate method. I suggest you brush up on some basics of designing a class.

  4. #4
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    thanks for ur valuable solution,
    but my intention is that the interface method x() should implement in main() method,
    is it possible.????????????


    Thanks
    Daya

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Since mgr implements a it needs to have the method declared. You can have an anonymous inner class of type interface but you don't want mgr to implement a then.

    Java Code:
    interface A{Object x();}
    
    public class Mgr{
      public static void main(String[] args){
        A aa = new A(){
          public Object x(){
            System.out.println("Creating an object");
            return new Object();
          }
        };
        aa.x();
      }
    }
    Is more along the lines of what you want. If you want mgr to also implement a thats fine, however it must still have the method declaration.


    In your original code, the following should work
    Java Code:
        package interfce;
    
        interface A
        {
    
            Object x();
    
        }
        public class Mgr implements A
        {
            public Object x(){}
            public static void main(String[] args)
            {
    
                A aa = new A()
                {
    
                    public Object x()
                    {
                    return new Mgr();
                    }
    
                };
    
            System.out.println(aa.x());
            }
    
        }
    Last edited by sunde887; 03-09-2011 at 09:45 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by Dayanand View Post
    thanks for ur valuable solution,
    but my intention is that the interface method x() should implement in main() method,
    is it possible.????????????


    Thanks
    Daya
    Then don't have Mgr implement A if you don't need it to?

Similar Threads

  1. Interfaces
    By blug in forum New To Java
    Replies: 1
    Last Post: 11-19-2010, 05:46 PM
  2. Interfaces (not GUI)
    By guilty in forum Advanced Java
    Replies: 1
    Last Post: 11-05-2010, 08:16 PM
  3. Interfaces
    By computerquip in forum New To Java
    Replies: 19
    Last Post: 09-08-2009, 05:58 PM
  4. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 10:57 PM
  5. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 06:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •