问题
In this tutorial (http://www.studytonight.com/java/object-and-classes) I read that a java class may optionally extend one parent class. By default, it will extend java.lang.Object.
Note: important statement that i was read that Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class.
according to note our normal java class should not extend other class like enum
(enum types cannot extend another class).but we can able to inherit one class. is this multiple inheritance.?
in java class can derived by extends
keyword. like this
class SomeClass
{ }
class MyClass extends SomeClass{}
How can all java classes by default extends java.lang.Object class without extends
keyword in java?
When our class extends some base class, it becomes multiple inheritance. I searched in stackoverflow, but still I am not clear. By default any class extends Object class. Doesn't it mean java supports multiple inheritance?
Can anybody clarify this with a simple example.
回答1:
- Every class, except for java.lang.Object, extends exactly one class.
- If you write
extends Something
, then your class extends Something. - If you don't write
extends Something
, then your class extends java.lang.Object. (the same as if you wroteextends Object
)
回答2:
If you don't extend any class, you will still extend Object. If you explicitely extend some class, than you extend just this class, but the extended class will in other turn extend Object by default. This way Object is always in class hierarchy.
来源:https://stackoverflow.com/questions/29672287/java-class-by-default-it-will-implicitly-extend-java-lang-object