-
Junior Member
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();
}
-
Junior Member
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
-
Junior Member
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.
-
Junior Member
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
-
Forum Rules