Is java.lang.Object superclass of all the custom class/objects inherited implicitly? I thought java didn't support multiple inheritance. The reason I ask is if I already inherit from another class in my custom class and again java is forcing implicit inheritance of java.lang.Object on top of it, is it not multiple inheritance?
Also, is java.lang.class Class also the superclass for all custom classes/Objects? If not, how in java reflections we can get the type of class for any class passed or call isInstance on any object?
Every class without an explicit superclass inherits from java.lang.Object and every other class inherits from it indirectly because when you go up the inheritance tree, you will finally end at a class without an explicit superclass and then at Object.
java.lang.Class is the superclass of all class objects (not of all objects!), for example of String.class.
Is java.lang.Object superclass of all
the custom class/objects inherited
implicitly?
Minor correction: regarding the following expression:
superclass of all the custom
class/objects
A class A is a superclass of another class B. Objects or instances of B do not have A as their superclass (since they are objects/object instances, not classes themselves.) The most appropriate description is that these objects are instances of class B, which has A as its superclass.
Now, to your question on java.lang.Object. It is the superclass of everything that can be instantiated by the java runtime. That includes both:
classes you write (custom classes), as well as
classes provided by the Java language itself.
I thought java didn't support multiple
inheritance.
It doesn't. It only support single class (or implementation) inheritance (in conjunction with multiple interface (or type) inheritance.
You are confusing inheritance with class hierarchy. Pls see further below.
The reason I ask is if I already
inherit from another class in my
custom class and again java is forcing
implicit inheritance of
java.lang.Object on top of it,
And that's fine with multiple inheritance. Say you have a class B that you wrote inheriting from a class already provided by Java (say class A). So there is a linear, transitive relation as follows:
java.lang.Object > A > B
Where > stands for immediate parent of. Any class inheriting from Object can only have one and only one immediate parent. At no point you'll have a class C for which the following constrain for any two classes A and B does not hold:
forAll A, B { A > B -> forAll C { C >
B <-> C == A } }
Meaning, for all classes A and B such that A is an immediate parent of B, then for all classes C, C can only be an immediate parent of B *if and only if C is itself A.
With single inheritance you can have (theoretically) an infinite inheritance chain A > B > C > ... so long as every class in the chain is preceeded by one and only one immediate parent. As a useful analogy, this resemble the predecessor relationship in natural numbers (0 preceedes 1 preceedes 2 which preceedes 3 ...).
Disclaimer: This is not stuff that you use in day-to-day programming life activities. However, knowing precisely what these things mean under the hood will tremendously help you get through object orientation.
is it not multiple inheritance?
No (see above.) As opposed to single inheritance - explained above - in multiple inheritance you can have multiple immediate parents (as seen in C++).
As we noticed in the definitions above, the transitive inheritance relation '>' is from one class to another. But with multiple inheritance, the relation is from a set of superclasses {A} to a single class {B}:
{A} > B -> forAll a {a in A <-> a > B}
And in C++ where you can have classes without parents, then the set {A} can be the null set. In java, you also have multiple inheritance, but limited to interfaces (or types.)
Also, is java.lang.class Class also
the superclass for all custom
classes/Objects?
No. It will help your learning if you take a visit to the Javadoc manuals which are available online. Look it up. Really.
If not, how in java reflections we can
get the type of class for any class
passed or call isInstance on any
object?
Because instances of java.lang.Class (and all classes used by java reflections) are meta-data descriptors for other classes. This has nothing to do with inheritance. What you are referring to here is known as meta-programming.
Furthermore, java.lang.Class is a final class; ergo, it cannot be the superclass of anything.
The function of java.lang.Class is to be a meta-data provider/descriptor for all instances and subclasses of java.lang.Object (including java.lang.Class itself.) The class field associated to each class (.ie. String.class) is a static, class-level field describing the meta-data associated to that class.
The meta-data contained/described by java.lang.Class contains meta-data accessor objects for methods, constructors and fields. Again, this has nothing to do with inheritance.
Inheritance =/= meta-programming.
Hope it helps.
Everything is an Object, that said you could see the structure as this:
Object
Animal
Cat
and not as this:
Object Animal
Cat
Where Cat extends both, it's not like this last example, but it's Cat that extends Animal which extends Object.
Multiple inheritance means that one class has multiple direct predecessors. If A inherits from B, and B inherits from C, this is not multiple inheritance.
I don't understand the second question ("java.lang.class Class"); you might want to rephrase that to clarify.
Object is the superclass of every other class. When your Child class inherits from your Parent class, it is not the child of Object, but it is still a descendant, because its parent is a descendant of Object.
So if you don't specify a superclass/parent, it will automatically be Object. If you do specify a parent, then Object will still be a superclass, because it is by definition a superclass of the parent class.
Class is not a superclass of each class, but the Object class specifies a means to get each class's Class with the getClass method.
The custom class that you inherit from itself inherits directly or implicitly from Object, so you indirectly inherit from Object. The chain goes YourObject -> MiddleObject -> java.lang.Object. No multiple inheritance necessary.
Multiple inheritance is when you directly inherit from two unrelated classes. For example, if you have a class PoliceDog that inherits directly from both Canine and PoliceOfficer, that would be multiple inheritence.
In that case, it's not multiple inheritence but multi-level
inheritance.
For example, let's say you have a class say, Class Animal,
this class will be extending Object class, by default and extends no
other class.
Also let's assume, you have a class which extends the
Class Animal, say Class Lion. In this case, the Class Lion will not be
directly inheriting the Object Class but since it inherits Class
Animal which inherits Object class, it also indirectly inherits
it.
Related
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.
Everybody knows that in java we can only extend "ONE" class.
But for the sake of understanding:
Any Java class implicitly extends java.lang.Object
If class A extends class B, wouldn't class A extend both class B and java.lang.Object implicitly ?
In such a case we are extending two classes by default.
Why is it allowed if Java doesn't support multiple inheritance ?
That would be a multilevel inheritance.
You are mistaking multiple to multilevel.
A->B->C //This is multilevel inheritance which you are talking about
Multiple inheritance is like (which is not possible in java)
A
| |
B C
Java doesn't support multiple inheritance that makes any ambiguous cases to fade away. But careful implementation of implement keyword for implementing does give feel of multiple inheritance
Conclusion:
Class A can extend a class B which extends class C. This is still single inheritance. All the classes form a tree, where the root is the Object class, and each class (except of Object) has exactly one direct super-class (or parent class)
No, Java prevents a class from directly extending more than one super class. Class A can extend a class B which extends class C. This is still single inheritance. All the classes form a tree, where the root is the Object class, and each class (except of Object) has exactly one direct super-class (or parent class), which is either Object or some other class.
Whenever class A extends class B, class A no longer extends Java.lang.Object, but however: Since every class extends Java.lang.Object as you said earlier, then class B would extend Java.lang.Object and therefore class A is still a subclass of that particular class.
"Implicitly extends Object by default" means that if you don't see the extends keyword in the class declaration it "invisibly but directly" extends Object. If you see the extends keyword, the class does not directly extend Object, but the class mentioned in the extends clause. Now you have to traverse that hierarchy and at one point, you'll find a parent class with no "extends", and there the implicit inheritance strikes again.
All classes in a hierarchy transitively extend Object, but only the root does it directly. Transitively because all subclasses inherit all the traits of their parents, including their parent classes and implemented interfaces.
If Java can only extend one class, and every class extends java.lang.Object, how can it extend another class? And how come every class doesn't have written extends Object?
If java can only extend one class, and every class extends java.lang.Object, how can it extend another class?
When you say A extends B then it means that A extends B and B extends Object. One class can inherit from another which can inherit from another and at the top of the chain is java.lang.Object. Java doesn't support multiple inheritance , but supports multi-level inheritance.
how come every class doesn't have written "extends Object" ?
All classes extend Object implicitly , so it will be redundant coding.
Every class in Java extends implicitly class Object, and you only have the permission to extend one more class, and infinite number of interfaces.
Same as having one default constructor for each class if you didn't write a constructor.
So when you say
class A{
}
it is actually looks like this:
class A extends Object{
A(){
super();
}
}
But when you say when you say
class B extends A
In this case class B will have the features of class A, and because A extends Object, class B will also inherit class Object.
A -> Object
B -> A
So
B -> Object
Since java.lang.Object is the parent of every object in Java, you don't have to explicitly write extends Object
You can always extend any class. If you need to have behavior of 2 classes - you need to use interfaces for that
The one and only that you are allowed to extend also extends the class Object ultimately.Hence you are not extending twice.
Multiple inheritance is not supported in Java, you can only inherit in a straight line (so to speak). You use interfaces though, which are IMO much more powerfull. I never needed multiple inheritance, but I make heavy use of interfaces.
Basically instead of creating a second class, you define an Interface, and then let some class implement it. This can be a different class or the same.
All classes are already derived from Objects, you don't need to write that specifically.
Each of the following can extend the object to the left:
Object -> Vehicle -> Car -> Coupe -> Camaro
So for example, Coupe is extending Car, and it is not (directly) extending Object... it's just that if you keep going up the chain you'll eventually get to Object.
You couldn't however, have Coupe extend both Car and Truck
Here is an example showing the class hierarchy of Java's java.lang.Number class.
http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
As you can see, Integer class inherits from Number, and Number inherits from Object. So you can inherit multiple classes by chaining them.
In object-oriented programming (OOP), inheritance describes a relationship between two types, or classes, of objects in which one is said to be a subtype or child of the other.
A -> B -> C -> D
The child inherits features of the parent, allowing for shared functionality. For example, one might create a variable class Animal with features such as eating etc.; then define a subtype Dog that inherits those features without having to explicitly program them, while adding new features like chasing cats.
Multiple inheritance allows programmers to use more than one hierarchy simultaneously, such as allowing Dog to inherit from Robot character and Animal and access features from within all of those classes.
A
/ \
C D
\ /
E
Java uses the first model and therefore inheriting from object and another class doesn't cause a problem because it is hierarchical in nature.
Object -> Animal -> Dog -> Pit Bull
The reason that every object does not need to extend Object is because it happens implicitly.
This question already has answers here:
Java doesn't support multiple inheritance but implicitly every class in java extends Object and allows one more [duplicate]
(8 answers)
Closed 10 years ago.
I have a query in java, that since Object class is the parent class for all, I want to know that is Object class is being implicitly extended by every class in java.
For example: if I make an object class B then I extend class A that is ok since in java, class can extend only a single class at most, but since my class B has implicitly extended the Object class then how can it extend class A? Please advise.
Only when a parent is not specified does it implicitly inherit off of Object, otherwise it only inherits off of the specified parent.
Side note:
This is allowed:
C
|
A
|
B
In the above case, replace C with Object to be in line with what you're asking.
This is not allowed:
C A
\ /
B
The inheritance can be as deep as you want it to be, but one object cannot have multiple parents.
Think of it as a family tree where individuals only have one parent. Each individual only has one parent, but all that parent's ancestors are also ancestors of the individual. That's what's meant when it is said that all classes extend Object: it is the most remote ancestor of all, though only the direct ancestor (parent) of those classes that are declared with extends Object or with no extends declaration at all.
I think you know the concept but you must be a bit confused. Think of a tree. Object class is the root of that tree. It has leaves and other nodes in between. No matter how your class hierarchy is designed, it will eventually an Object because its root is that. So you can extend any class (not abstract or Interface), without thinking of Object class. Because Object extention will be done automatically for you.
Here, I will suggest, the keyword is EXPLICIT. You can EXPLICITLY extend only one class. But if you don't do that Java will implicitly extend your class with Object class. If you have done that Object must be somewhere up in the hierarchy of the inherited class.
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.