In: Computer Science
3.
Which statement about methods in an interface is true?
A.)All methods in an interface must be explicitly declared as
private or public.
B.)All methods in an interface are automatically public.
C.)All methods in an interface are automatically private.
D.)All methods in an interface are automatically static.
Answer::: B.)All methods in an interface are automatically public.
Explaination::
An interface can have methods and variables, but the methods declared in an interface are by default abstract i.e only method signature, no body.
Syntax::
interface <interface_name> { // declare constant fields // declare methods that abstract // by default. }
We use interface to achieve abstraction.Also java language does not support multiple inheritance but by using interface it is used to achieve multiple inheritance.
All the fields in an interface can be abstract,static,final and public.But all methods in an interface are public.
Simple Example::
interface Moveable
{
int AVG-SPEED = 40;
void move();
}
class Vehicle implements Moveable
{
public void move()
{
System .out. print in ("Average speed is"+AVG-SPEED");
}
public static void main (String[] arg)
{
Vehicle vc = new Vehicle();
vc.move();
}
}
Output:: Average speed is 40.
Here,we have define a interface which contains one variable AVG-SPEED and a method move() which is automatically becomes public.
Class Vehicle implements Movable interface and overrides the move() method and by creating a object of class Vehicle we are calling move() method.