Java class by default, it will implicitly extend java.lang.Object [duplicate]

社会主义新天地 提交于 2020-01-21 08:40:07

问题


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 wrote extends 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!