interfaces not inherited from object class and there’s no common or root interface which implicitly inherited by all interfaces either.
Note: For each public method in Object class, there’s an implicit abstract and public method declared in each interface which doesn’t have direct super interfaces. This standard is defined by JLS (Java Language Specification) as
“If an interface has no direct super interfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.”
Please check the below example how Object class methods are available in interface:
Example
interface Test { @Override public boolean equals(Object obj); @Override public int hashCode(); @Override public String toString(); } public class Main { public static void main(String[] args) { Test test = null; System.out.println(test.equals(null)); System.out.println(test.hashCode()); System.out.println(test.toString()); } } |
Output
Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:25) |
Java interview questions on Inheritance
- Why multiple inheritance is not supported in java?
- How to implement multiple inheritance in java?
- Are interfaces also inherited from Object class?
- Why an interface cannot have constructor in java?
- How do you restrict a member of a class from inheriting to it’s sub classes?
- Can a class extend itself in java?
- Are constructors inherited in java?
- What happens if both superclass and subclass have a field with same name?
- Are static members inherited to subclasses in java?