power_settings_new

can a class be extended by interface in java? why?

by
asked
0 votes
28 views
I appared for an interview with on of the IT companies. They asked me "can a class be extended by interface in java? why?". Of course we know that it can not be extended. But I had no justification for why.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

answered by (171 points)
0 votes

No, Interface only has declaration. Interface does not have definations. So class can have declaration and definations. If you try to extend class by interface, you will break basic rule. So ultimatly compiler will throw an error.

 

Valid:

import java.rmi.Remote;
 
public interface MyInterface extends Remote{
 
}
 
and
 
import org.openqa.selenium.remote.RemoteWebDriver;
 
public class myClass extends RemoteWebDriver{
 
}
 
invalid:
 
import org.openqa.selenium.remote.RemoteWebDriver;
 
public interface myInterface extends RemoteWebDriver{
 
}

Hope above example helps.

add
...