PDA

View Full Version : interface pgm in java



navaneethan
August 5th, 2010, 06:10 PM
interface First{
int firstMethod();
int secondMethod();
}
interface Second extends First{
int thirdMethod();
}
class MyClass implements Second{
public int thirdMethod()
{
System.out.println("THIS IS THE IMPLEMENTED INTERFACE");
}
}
public class MyMain{
public static void main(String args[])
{
MyClass m = new MyClass();
m.thirdMethod();
}
}





Here I couldn't get the expected output may i know the problem here and give some suggestion about this

Some Penguin
August 5th, 2010, 06:19 PM
Your problem appears to be that you've not bothered to learn the actual meaning of keywords like 'class', 'interface', and 'extends'. The code as presented won't compile, because your class lacks implementations for methods belonging to the interface that it supposedly implements.

Please read the sticky threads for information on learning languages so you can develop a more logical approach to learning than whatever it is that you happen to be doing now.

Queue29
August 5th, 2010, 07:28 PM
What do you expect to happen when you call firstMethod() and secondMethod()?

You failed to implement them in your base class. The code won't compile, because it's incomplete.



public static void main(String args[])
{
MyClass m = new MyClass();
m.thirdMethod();
m.firstMethod(); // ???
m.secondMethod(); // ?????
}

KdotJ
August 6th, 2010, 12:49 AM
Your problem appears to be that you've not bothered to learn the actual meaning of keywords like 'class', 'interface', and 'extends'.

Lol indeed.
To the OP, is this homework of some sort? How long have you been studying Java (or programming for that matter)?

navaneethan
August 6th, 2010, 03:23 AM
This is not mine homework because i just got the output and while i understand the reason for this output i got stuck thats why sharing here?
i am not a school boy to do homework ;-) as well as i am not the expert like you ;-) just to know the exact reason i stepped here ;-) please mind it ;-)

KdotJ
August 6th, 2010, 02:01 PM
Ok well, do you understand what an interface is? Do you understand how they are used?
Do you know what the keyword 'extends' means?

navaneethan
August 6th, 2010, 04:55 PM
well it was a nice explanation done it :-)

KdotJ
August 6th, 2010, 05:30 PM
I'm very confused but hey.So have you solved your problem?

navaneethan
August 7th, 2010, 05:24 AM
yes done dude I have understood what i want :-) thanks for your participation If anything wrong in my side meant i apologise

KdotJ
August 7th, 2010, 08:38 PM
Well done on the fix