I'm new on Java, reading the Sun basic tutorial and see "The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. " I wonder how can Object be the root parent of the class I defined if my class did not inherit from other classes.
If you continued reading on the same page
(emphasis mine):
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.
This is an implicit behavior. All of your classes extend Object (directly or not).
public class MyClass is equivalent to public class Myclass extends Object
That's the "magic" done by the Java compiler: when you write
public class MyClass {
...
}
Java compiler sees it as
public class MyClass extends java.lang.Object {
...
}
It works that way because the Java Language Specification (specifically section 8.1.4) says so:
Given a (possibly generic) class declaration for C<F1,...,Fn> (n ≥ 0, C ≠ Object), the direct superclass of the class type C<F1,...,Fn> is the type given in the extends clause of the declaration of C if an extends clause is present, or Object otherwise.
Related
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.
In Java Reflect API, in .isAssignableFrom methods in Class has its javadocs saying that it will return true if "the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter".
The question is whether .isAssignableFrom will return true if on a class that implements Interface, or it only returns true when "extends" is used?
In other words, what will happen and why in case:
public class MyClass implements MyInterface{}
MyInterface.isAssignable(MyClass.class) == false/true ?
It returns true.
That are two ways to tell this from the Javadoc:
the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter
The term superinterface is defined in the Java Language Specification as follows:
The optional implements clause in a class declaration lists the names of interfaces that are direct superinterfaces of the class being declared.
Therefore, MyInterface is a superinterface of MyClass, and therefore MyInterface.class.isAssignableFrom(MyClass.class) is true.
The other way to tell is the next paragraph of the javadoc:
Specifically, this method tests whether the type represented by the
specified Class parameter can be converted to the type
represented by this Class object via an identity conversion
or via a widening reference conversion. See The Java Language
Specification, sections 5.1.1 and 5.1.4 , for details.
which matches because
MyInterface i = new MyClass();
compiles.
I'm reading Java Inheritance Docs and got this paragraph, but I don't what is mean of this. Can any body explain?
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.
Edited
Another point related to this question:
And due to Object is superclass, NetBeans show me extra method list when I try to call any Member of class with object reference? All those methods are declared in Object class?
It means that,in java the Object class is the parent class of all the classes by default. In other words, it is the topmost class or the base class of java. All other classes inherit the properties of Object class.
Meaning of implicitly - suggested though not directly expressed.
If samething applied in Java classes. Consider there is a class called Test.
If you write
public class Test {....}
That is equivalent to
public class Test extends Object {....}
Though you didn't write it, it's equivalent to it. You need not to write extends Object manually. Internally JVM treats that your class is extends Object. Since the part after extends is your class, here Object is your super class.
But,
When you write
public class Test extends BigTest{....}
Things changed now. You are telling BigTest is my parent class. That means you are implicitly writing yourself that BigTest is my parent. Interesting part here is though BigTest is your direct Parent class, internally Object also your Parent.
So now you have 2 parent classes. One is BigTest which you mentioned and other is Object. If you didn't mention anything, only Object is your Parent.
Edit :
This is the reason that you will see extra list of method when you will try to call any member of class with object-reference. Those methods are declared in Object class.
why java do this? any feature of this?
Yes. There are benefits of it. Main reason is to reduce the code duplication.
Continue reading here .... Why Object as super class in Java ?
It means that the following two classes are the same:
class MyClass {
}
class MyClass extends Object {
}
If a class definition doesn't specify extends, the class implicitly extends Object.
The Object class comes with the JRE, you use any class without it. Java does this so you can have a reference to any object e.g.
Object o = x;
This works because no matter what x is, it is also an Object.
Note: even int[] class is a sub-class of Object.
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.
Just had this in a unit test. So:
abstract class A {...}
class B extends A {...}
A a = new B();
Pretty much doubt it's viable, but I'm totally confused at the moment. The content of the classes is irrelevant.
yes this concept is known as polymorphism in java . a parent can contains reference of child class. and parent can be either interface or abstruct class. even you need not to cast . for example .
a animal can be horse or cat , here animal is an interface or abstract class.
i recommend (SCJP Sun Certified Programmer -kathy sierra)book has lot of concepts related to java language.
Its valid in java. this is called implicit up casting (JVM casts sub type to super type object) where reference of super type can refer to sub type but with super type reference you can only get access to only those variables and methods which are declared/defined in super type.
Yes this is valid in java
abstract class A {...}
class B extends A {...}
A a = new B();
Yes this is a valid java assignment statement.
A a = (A) new B();
In java a child object can be referenced from the parent class.
It's valid.
As per Java's fundamental rule "Super class reference (A a) can refer sub class object (b= new B())".
Interface - every method is always public and abstract whether you are declaring it or not. You can’t declare an interface method with the following modifiers. Every variable present inside interface is always public static and final whether we are declaring or not. You can’t declare an interface variable with the following modifiers, private, protected, transient and volatile. The variables present within abstract class don’t have to be public, static and final. There are no restrictions on abstract class variable modifiers. For abstract class variable are not required to perform initialization at the time of declaration. An abstract class can declare instance and static blocks. Inside interface we can’t declare instance and static blocks otherwise we will get compile time error. Interface can’t declare constructors. Just keep these fun facts in mind.