Results 1 to 4 of 4

Thread: Interface methods body

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Answers
    16

    Interface methods body

    In interface i wrote two methods ie without body and i want to write the body one in one class other in another is it possible

    Answer: Actually in an interface I wrote two methods without body ie definitions and I want two implement ie writing total body and using it in one method in one class and other method in other class is it possible.

    see below example and explain the problem

    take windows application and take a button control

    interface i
    {
    void read();
    void print();
    }
    class test : i
    {
    public void read()
    {
    MessageBox.Show("from read...test");
    }
    }
    class best : i
    {
    public void print()
    {
    MessageBox.Show("from print...best");
    }
    }

    private void button1_Click(object sender, EventArgs e)
    {
    test t = new test();
    best b = new best();
    i x = new test();
    x.read();
    i y = new best();
    y.print();
    }


  2. #2
    Junior Member
    Join Date
    Jul 2006
    Answers
    22

    Re: Interface methods body

    Its not possible to declare one interface methods in different classes.
    If you are implementing any interface,you should implement all the methods in that interface.means full implementation should be done


  3. #3
    Junior Member
    Join Date
    Nov 2007
    Answers
    9

    Post Re: Interface methods body

    You can not do this... I think the only way to achieve, what you are trying to do is, create two different interfaces.


  4. #4
    Junior Member
    Join Date
    Dec 2007
    Answers
    13

    Re: Interface methods body

    You can override two methods of interface in two different classes by declaring them abstract.
    e.g.
    interface i
    {
    void read();
    void print();
    }

    abstract class test implements i
    {
    public void read()
    {
    MessageBox.Show("from read...test");
    }
    }

    abstract class best implements i
    {
    public void print()
    {
    MessageBox.Show("from print...best");
    }
    }


    You can not instantiate these classes.
    to instantiate them, you need to again inherit these classes.

    e.g.
    class test_child inherits test
    {
    public void print()
    {
    }
    }

    abstract class best_child inherits best
    {
    public void print()
    {
    }
    }

    private void button1_Click(object sender, EventArgs e)
    {
    test_child t = new test();
    best_child b = new best();
    i x = new test_child();
    x.read();
    i y = new best_child();
    y.print();
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact