Interfaces and Abstract Classes in Java - java

Does interfaces and abstract classes in Java extend base Object class? Logical answer which I can think of is no, but if interface does not extend Object class, then can please someone explain me the below code:
interface A {
#Override
public int hashCode();
#Override
public String toString();
}
In above case, A interface is the new interface declared. Yet, no class is implementing it. Then how come the methods from Object class visible in the interface?
If it doesn't extend Object class, then why I can't declare following method in the interface:
public Class getClass();
When I tried to declare this method in the interface, it says that it can't override this method, inherited from Object class, as it is declared as final method in Object class.

Interface could only extend some other interface and no - there is no some root interface. Abstract class, as any other class in Java, has Object as its ancestor - while it may not be a direct parent.
Methods, marked as final, could not be overriden in successor classes. As your class has Object as its ancestor - this rule also applies here.

The reason this is can be found in JLS $9.2
If an interface has no direct superinterfaces, 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.
It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
And as seen in Object#getClass:
public final Class<?> getClass()
Interfaces do not inherit from Object, but they do mimick the methods available by implicitly adding them. Likewise here, a final method in that base interface can't be overridden in your self-defined interface.

Related

How can I override object methods inside interface?

All classes in java extend the Object class implicitly. But that doesn't concern interfaces. Interfaces can only extend other interfaces, but no classes. However, I can override object class methods inside my interface.
public interface NewInterface {
#Override
boolean equals(Object var1);
#Override
int hashCode();
}
Can you please explain in simple words how is this even possible? Is there any use case for this?
Interface is a just contract. It says that all classes that inherits interface should implement these methods. Interface cannot have implementation. It is possible to override a class that implements this interface.
However, from Java 8 you can define static methods in interfaces in addition to default methods.
UPDATE:
The members of an interface are:
Those members declared in the interface.
Those members inherited from direct superinterfaces.
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in Object, . It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
Now it is clear that all superinterface have abstract member method corresponding to each public instance method declared in Object .
Read more about interface members here

Interfaces implicitly declaring public methods of Object class? [duplicate]

This question already has an answer here:
How methods of Object class be visible through interface?
(1 answer)
Closed 1 year ago.
According to The Java Language Specification, Java SE 16 Edition (JLS) §9.2 Interface Members:
If an interface has no direct superinterface types, 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 (§4.3.2), unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
Why does any top-level Interface “implicitly” declare the public methods of the Object class? What is the purpose of this design?
What is the purpose of this design?
Because you want to be able to call all the Object methods ( toString, equals, hashCode, and such) on every object of any type.
interface Foo {
}
Foo foo = ...;
foo.equals(otherFoo);
Doesn't matter if I actually declared the equals method in the interface.
Every class is implicitly a subclass of Object
See: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html for more
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

Do interface references require a cast to be assigned to a class Object reference?

While revising for the Java SE 8 Programmer I (formerly OCA) Certification exam, I came across the following statement:
"A reference to an interface requires an explicit cast to be assigned
to a reference of any class, even one that implements the interface.
An interface reference requires an explicit cast to be assigned to a
class reference."
I think this is slightly inaccurate as it is possible to assign a reference to an interface to a reference of the class Object without an explicit cast.
interface Animal {}
public class Dog implements Animal {
public static void main(String[] args) {
Animal animal = new Dog();
Dog dog = animal; // Doesn't compile - requires explicit cast to Dog
Object o = animal; // Compiles
}
}
Is this due to a link between the Object class and interfaces akin to that described in this answer relating to the class file format?
From this answer relating to accessing the Java Object class methods using an interface reference, I've summarized that:
Using an interface reference, you can only access methods defined in
the interface but not class specific methods.
Interfaces have "Hidden Declared" methods of class Object.
You can access methods of the Object class via any interface reference because, although an interface
doesn't extend from the Object class, every root interface in Java has implicit declarations of
methods corresponding to each method in the Object class.
JLS §9.2 - Interface members:
If an interface has no direct superinterfaces, 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.

Why abstract does not work for interface implicit methods?

While reading JLS Specification for Interfaces I came across following phrase:
If an interface has no direct superinterfaces, 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.
So My question is when we implement an interface why we are not forced to override implicit methods which are declared in Object class even though they are implicitly defined as abstract in Interface.
Hope I put myself correctly.
Thanks.
All classes implicitly extend Object, either directly or through some chain of superclasses. Thus, you don't have to explicitly override the implicit methods declared in an interface because you inherited the implementations from Object.
The point of an interface is to force you to implement some method. The reason everything extends Object is that we want to have some way of dealing with all classes independent of implementation. There is no reason that each class should implement something like getClass(), because the behaviour is always going to be the same.

Why do interfaces extend Object, according to the class file format?

Why does the JVM specification state that interfaces must have a super_class of java/lang/Object, even though interfaces do not extend java/lang/Object?
I'm specifically referring to §4.1 of the JVM spec, where it says:
For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object.
yet in §9.2 of the JLS, it says that interfaces do not extend Object. Instead a implicitly created abstract method is declared which matches each public method in the Object class:
If an interface has no direct superinterfaces, 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.
As mentioned in §9.2 :
If an interface has no direct superinterfaces, 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.
Hence , we see that , Although an interface having no direct superinterface doesn't explicitly extends Object but still it has a link with Object class internally as it is used by the compiler to insert abstract methods with same signature and return type and throws clause as that of public methods in Object class, within the interface. That's why For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object. This is the reason that an interface reference variable can successfully call public instance methods for example toString() method of Object . For example, consider the code given below:
interface MyInterface
{}
public class InterfaceTest implements MyInterface
{
public static void main(String[] args)
{
MyInterface mInterface = new InterfaceTest();
System.out.println(mInterface.toString());//Compiles successfully. Although toString() is not declared within MyInterface
}
}
The above code compiles successfully even though toString() method (Which is the method of Object) is not declared within MyInterface. Above code is providing following output on my System:
InterfaceTest#1ba34f2
The output may vary from system to system..
What you see in the JVM spec is basically the concrete implementation of the behavior specified by the JLS - just like classes implement interfaces and have implementation details.

Categories

Resources