Classes that don't inherit Object class - java

Are there any classes that don't inherit Object as SuperClass or maybe have become Obsolete/deprecated?

According to Java Object superclass, java.lang.Object does not extend Object.
Other than that, all classes, i.e.
class ClassName {
//some stuff
}
implicitly extend Object class, if they don't extend any other super-class.
Interfaces, on the other hand, do not extend Object, as Interface, by definition, can not extend Class.
Also, Interfaces can not contain callable methods, nor can objects be instantiated from them. When interfaces are finally implemented, the implementing class will necessarily extend Object (and, no, Object doesn't implement or extend any other entity/class/interface).

According to java.lang.Object javadoc
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.
So, all objects in Java extends it directly or indirectly.

All Java classes inherit java.lang.Object (directly - by default, or via parents). If some class or method become deprecated with some platform release, it is always reflected in appropriate JavaDoc.

Are there any classes that don't inherit Object as SuperClass
There is exactly one of those, and it is java.lang.Object itself. Also all interfaces.
or maybe have become Obsolete/deprecated?
There are plenty of those. See the Javadoc.

Related

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.

Class without java.lang.Object as a superclass

We know that by default, every class inherits ultimately from the java.lang.Object class, which is why methods such as toString are readily available to every class. Therefore, a generic class is effectively as follows:
public class Foo extends java.lang.Object {
public Foo() {super(); }
}
However, is it at all possible to create a class which does NOT automatically inherit from the Object class, and thus, has no superclass? And if so, what would be the effect?
We can't write class without having java.lang.object as superclass. Compiler will automatically extend the object class.Only the object class itself and interfaces are the ones which do not extend object class.
No, that is not possible. From the 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.
I do not believe that you can have a class that does not inherit Object. Quoting from Object as a Superclass..
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. Every class you use or write inherits the
instance methods of Object. You need not use any of these methods,
but, if you choose to do so, you may need to override them with code
that is specific to your class.
In cases where the inheritance is not explicitly stated, it is implicitly stated. Now, inheritance will obviously not form a cycle. The Object class is the only one which inherits from nobody, it's the top of hierarchy.

Interface does not have constructor, then how can it be inherited?

As I know the subclass constructor calls the super class constructor by using super();.
But since interface doesn't have any constructor, how can inheritance take place?
But since interface doesn't have any constructor how can inheritance take place??
Easy, an interface cannot have any instance fields so there is nothing to construct. You cannot place in code in an interface (up to Java 7 anyway) so there is nothing which needs to be called.
The interface is a contract, defining what methods mush be offered by the implementation. A class doesn't inherit an interface but implements it.
From the specification :
This type has no implementation, but otherwise unrelated classes can
implement it by providing implementations for its abstract methods.
Interfaces (also known as Service Contracts) are implemented, not constructed. They define a set of methods (Services) that a class provides, so a client knows what can he expect of the implementing class regardless of the actual type implementing the interface. The constructor is related to this particular instance of a given type, implementing the interface.
IYourObject yourObject = new YourObject();
On the other hand, interface inheritance is also by extension. It "adds" the methods of an interface to another one and allow the possibility of switching the interface type of an object amongst the different interfaces in the "hierarchy".
Interface is not inherited - it is rather implemented
Interface doesn't contain constructor because of the following reasons:
The data member of the interface are already initialized .
Constructors of the special defined methods which are not permitted to defined and moreover interface data member are static.

Why does java.lang.Math extend java.lang.Object?

According to the Java API for the Math class, Math extends the Object class:
public final class Math extends Object
However, Math does not inherit Object's methods, and you also cannot construct a Math object (EDIT: this statement is false and partly false; see below).
My question is then why is this done? If it is an object, then it should have the bare-bones methods from Object. If it is not an object, then I feel like it should not be extending Object.
And, assuming there is a good reason for extending Object, how is it done? In other words, how are the inherited methods and constructor suppressed?
Math is the foremost example in my mind of this kind of static definition class (I guess you'd call it that?), but this question may also apply to classes that have a similar purpose.
EDIT: So I'm aware that all classes implicitly extend Object. What's bothering me is that to me there's a logical disconnect between the notion of an "object" in theory-- something with a state and associated functionality-- and what's being done here.
Object is the super class of everything. All classes extend Object
Math does inherit Object's methods. Every class in Java ultimately has to inherit from Object. But since you can't construct Math objects, it doesn't matter. The methods are inherited, but without being able to construct an instance you can't ever use them from Math.
Every class implicitly extends Object unless they extend another class (Java doesn't allow multiple inheritance).
Still! by extending another class you're, in the very end, extending Object, because the last class that doesn't extend another particular class extends Object.
The concept behind this "default inheritance" can be extracted directly from Object's JavaDoc:
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.
EDIT: Math does inherit Object's methods. It does not override them tough and the calls are derived to the implementations of the superclass.
You should pay attention to an important aspect of the Math class. The constants and methods it defines are mainly static. Mathematical functions do not depend of a particular instance of the class because those calculations are independent of the context.
You have no particular reason to create an instance of the Math class and that's why its constructor is not visible. You cannot do Math m = new Math() and that's why you don't see methods such as equals or hashcode. In particular since the class is final and can't be extended, you cannot create a sublclass that defines them. IMHO it would be pointless.
Object is the super class of every java class. An you are mistaken that Math does inherit object's methods.
If you want more see the source.
http://www.docjar.com/html/api/java/lang/Math.java.html

What constitutes indirect inheritance?

What do we mean when we say "All classes either directly or indirectly inherit from class Object"? What constitutes a class indirectly inheriting the methods declared in its indirect superclass?
Can we say that a class indirectly inherits methods declared in one of it's indirect superclasses, regardless of the fact that the method defined in the superclass may have been overridden whilst propagating down the inheritance hierarchy, thus comments such as the above are based on the fact that the subclass inherits some method with the same signature as that declared in the superclass.
Alternately, can we only ever say that a class indirectly inherits the methods declared in one of it's indirect superclass if and only if the class is inheriting THE method declared in it's indirect superclass, that is the method has not been overridden whilst propagating down the inheritance hierarchy, and the class is inheriting the method with the same implementation as that defined in it's indirect superclass.
By indirectly inheriting, they mean that you extend another class that in itself extends Object. Directly inheriting from Object would mean that you either explicitly stated extends Object on your class signature, or you didn't define another class it should extend (in which case it extends Object directly anyway by definition.)
In terms of specific methods, you only inherit the one that was "last overriden" in the hierarchy. So if I'm inheriting from a class that inherits toString(), I'll only inherit the overriden version, not the original from Object.
Class1 indirectly inherits Class2 when you use "is-a" more than once to describe their relationship (remember that "is-a" relationship is transitive).
Example: a red-apple is-a apple is-a fruit is-a consumable. In this example, red-apple indirectly inherits from fruit and consumable. red-apple directly inherits from apple (only one is-a transition used)
The question about indirect inheritance of class was already answered. So I add on methods.
I think what is important here is the difference between class interface (signatures of available methods, not java mechanism of interfaces) and class behavior - the implementations of available methods. In your question about methods you can say that class indirectly inherits methods regardless of whether they were overridden by intermediate classes, and without any other details it means inheriting interface. To set the context of phrase to behavior meaning you must explicitly state that implementation of method X is taken from class Y.

Categories

Resources