Reputation: 453
So I looked online through a number of resources to understand Java interfaces. I believe I have a good general understanding of them but when programming them I am a bit confused...
I created an interface called A and have the following inside...
public interface A {
public int sum(int first, int second);
}
I then created a class called B.
public class B implements A {
public static void main(String [] args){
int result = sum(3, 5);
}
public int sum(int first, int second) {
int total = first + second;
return total;
}
}
Now what I am trying to figure out is how can I properly call / use the method "sum". In Eclipse I'm getting an error for the line "int result = sum(3, 5);" and it is telling me to make the method static. If I make it static, then the method needs to match it in the interface. However, I am not able to use static methods in an interface?
Any help is appreciated and thank you for your time to read about my problems.
Upvotes: 1
Views: 737
Reputation: 59
The problem seems to be very simple , either make your sum method static or create an instance of class B and use it to call the sum method:
B b=new B();
int result=b.sum(3,5);
or
just write static before your sum method
for eg:
public static int sum(int first, int second)
{
int total = first + second;
return total;
}
Upvotes: 0
Reputation: 843
I will try to make this clear by giving another example:
Make a classed named Animal.
public interface Animal {
String speak();
}
Now make a class named Cat
public class Cat implements Animal {
public String speak() {
return "meow"
}
}
And a class named Dog
public class Dog implements Animal {
public String speak() {
return "woof"
}
}
Now you can do this
public String speak(Animal a) {
System.out.printf("%s\n", a.speak());
}
Cat cat = new Animal();
Dog dog = new Animal();
speak(cat); //prints meow
speak(dog); //prints woof
That means Cat
is an Animal
and Dog
is is an Animal
too, so you can pass a Cat
or Dog
object to a function that takes Animal
arguments.
It's like Inheritance, but since in Java a class can only inherit from one other class, you can use Interfaces to get around that. You only declare methods in the Interface; you have to define them in the class that implements it.
It can also be used for things like ActionListeners
or MouseListeners
. You can have a GUI class that implements it, then have a function that handles all of your ActionEvents
like button clicking and mouse clicking.
Upvotes: 0
Reputation: 500
It's the issue of static, not interface. You can not call non-static method from static method. You can call sum method by creating its object.
like,
int result = new B.sum(3, 5);
inside static method.
Upvotes: 0
Reputation: 15406
You cannot call sum()
from your main method because sum is an instance method, not static. It needs to be called from an instance of a class.
You need to instantiate your class:
public static void main(String [] args) {
B b = new B();
int result = b.sum(3, 5);
}
Upvotes: 2
Reputation: 24780
The issue you have is not of interface but of static
method.
main
is a static
method. That means it is not linked to an object/instance, but to the class itself.
Since you want to use sum
, which is an instance method, you need first to create an object to call its method.
A a = new B();
int result = a.sum(5, 6);
Usually, instance methods are more linked to the object status, while static methods are more like "procedures" in non OO languages. In the case of sum
, your method would make more sense as static. But, if you use B
to wrap a value (status), and use sum to add to your internal status, this would end (in a more OO friendly way).
A a = new B(5);
a.sum(6);
int result = a.getValue();
Note that both approachs are valid and both compile in Java, it is just a matter of selecting the modifiers that make more sense in each occasion.
Upvotes: 5
Reputation: 46398
By creating an instance of B like this:
A a = new B();
int result = a.sum(3, 5);
Upvotes: 1
Reputation: 347184
public class B implements A {
public static void main(String [] args){
int result = new B.sum(3, 5);
// Create an instance of B so you can access
// the non-static method from a static reference
// Or if you want to see the power of the interface...
A a = new B();
int result = a.sum(3, 5);
}
public int sum(int first, int second) {
int total = first + second;
return total;
}
}
Upvotes: 1