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

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.

Related

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.

Can "override abstract methods" be substituted with "implement abstract methods" in Java inheritance?

In Java inheritance, can the expression "overriding an abstract method" be used interchangeably with the expression "implementing an abstract method"?
I came across the following question in the book "OCA Java SE 8 Programmer I Study Guide" in Chapter 5, page 296, question #15:
Q: Which of the following is true about a concrete subclass? (Choose all that apply)
A. A concrete subclass can be declared as abstract.
B. A concrete subclass must implement all inherited abstract methods.
C. A concrete subclass must implement all methods defined in an inherited interface.
D. A concrete subclass cannot be marked as final.
E. Abstract methods cannot be overridden by a concrete subclass.
My answer was B & E. But the book says, the correct answer is only B.
My question is specifically about the option E. The book says, option E is incorrect because abstract methods must be overridden by a concrete subclass. My initial thought was abstract methods must be implemented before being overridden by a subclass. So why is option be incorrect?
Let me explain each points one at a time.
A. A concrete subclass can be declared as abstract.
Ans: Incorrect.
By the definition, concrete classes are those which can be instantiated and this class can also extends the base abstract class.
B. A concrete subclass must implement all inherited abstract methods.
Ans: Correct
If the methods are marked as abstracts in the base class, then the child class extending base class should make sure that all the abstract methods are implemented. You will otherwise get a compile time error.
C. A concrete subclass must implement all methods defined in an inherited interface.
Ans: Incorrect
this is only the case when the concrete subclass has directly implemented the interface by this class itself.
But in this case, base class may have implemented an inherited interface .Hence, its not mandatory for concrete sub-class to implement all methods that are there in base class as a result of inheritance from the interface.
D. A concrete subclass cannot be marked as final.
Ans: Incorrect
If a subclass is marked as final, then this class can no longer be extended by any other available sub-classes. Marking a class with a final keyword is particularly used to achieve Immutability.
With Immutable classes, We can call accessor methods (e.g. getter methods), copy the objects, or pass the objects around — but no method should allow modifying the state of the object. Examples: String, wrapper classes like Float, Integer.etc.
E. Abstract methods cannot be overridden by a concrete subclass.
Ans: Incorrect
By the definition, Abstract methods are methods that are only declared but contains no body or implementation.And since we cannot instantiate abstract class, it is up-to sub classes to provide the implementations for these defined abstract methods.Hence abstract methods are not optional but mandatorily be overridden by a inheriting subclass.
An abstract method is a method that is declared, but contains no implementation.
you can override both abstract and normal methods inside an abstract class.
only methods declared as final cannot be overridden.

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.

super() method not giving error when no parent class is defined

class child
{
child()
{
super();
System.out.println("Hello");
}
public static void main(String arg[])
{
child obj=new child();
}
}
In this code when I a create an object of class child , the child constructor is called. But why is it not giving error as there is no parent class. What does the super() do here?
Whose constructor is the super() keyword calling?
In Java every object implicitly extends Object. Calling super here will just call Object's constructor. On another note you should really abide by naming conventions such as capitalizing class names.
It is calling the constructor of the Object class as all object in java extends by default to the Object class.
From documentation:
Class Object is the root of the class hierarchy. Every class has Object as a superclass.
All objects, including arrays, implement the methods of this class.
You're invoking the Object method, from which all other classes descend eventually.
First up, clarifying the class hierarchy in this situation, the inheritance section in the Java tutorial states:
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.
Then, for the tutorial stuff on using super:
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
Every class we create in Java is an implicit descendant of "Object" object (in other words, subclass of Object). Hence when ever you are making a call to super() it implicitly calls the constructor of Object class. The basic reason to have this feature is to generalize the common features like:
synchnronizaton - like wati()
object identity - like hashcode(), equals()
and many more.
Thanks,
JK
The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class.
All objects, including arrays, implement the methods of this class. Thats why super() in your case is actually calling the constructor of Object class.

Interfaces and Abstract Classes in 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.

Categories

Resources