知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

      • Theory:Introduction OOP
      • Theory:Defining classes
      • Theory:Instance methods
      • Thoery:Constructor
      • Theory:Package
      • Theory:Access modifiers
      • Theory:Getters and setters
      • Theory:Inheritance
      • Theory:Protected modifier
      • Theory:Referencing subclass objects
      • Theory:The Object class
        • Instantiating the Object class
        • Methods provided by the Object class
        • Conclusion
      • Theory:Objects
      • Theory:Interface
      • Theory:Multiple constructors
      • Theory:The keyword super
      • Theory:Static members
      • Theory:Hiding and overriding
      • Theory:Polymorphism
      • Theory:Abstract class
      • Theory:Abstract class vs interface
      • Theory:Anonymous classes
      • Theory:Anonymous classes properties
    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java OOP
Jim
2022-05-14
目录

Theory:The Object class

The Java Standard Library has a class named Object that is the default parent of all standard classes and your custom classes. Every class extends this one implicitly, therefore it's a root of inheritance in Java programs. The class belongs to the java.lang package that is imported by default.

# Instantiating the Object class

Let's create an instance of the Object class.

Object anObject = new Object();
1

The Object class can refer to an instance of any class because any instance is a kind of Object (upcasting).

Long number = 1_000_000L;
Object obj1 = number; // an instance of Long can be cast to Object

String str = "str";
Object obj2 = str; // the same with the instance of String
1
2
3
4
5

When we declare a class, we can explicitly extend the Object class. However, there is no point, since the extension is already done implicitly. We advise you to avoid redundancy in your code, but here's an example, just in case:

class A extends Object { }
1

In your own solutions, it is enough to write class A { }.

# Methods provided by the Object class

The Object class provides some common methods to all subclasses. It has nine instance methods (excluding overloaded methods) which can be divided into four groups:

  • threads synchronization: wait, notify, notifyAll;
  • object identity: hashCode, equals;
  • object management: finalize, clone, getClass;
  • human-readable representation: toString;

This way of grouping methods isn't perfect, but it can help you remember them. Here's a more detailed explanation of the methods:

  • The first group of methods (wait, notify, notifyAll) are for working in multithreaded applications.
  • hashCode returns a hash code value for the object.
  • equals indicates whether some other object is "equal to" this particular one.
  • finalize is called by the garbage collector (GC) on an object when the GC wants to clean it up. (Note: this method has been deprecated as of JDK 9).
  • clone creates and returns a copy of the object.
  • getClass returns an instance of Class, which has information about the runtime class.
  • toString returns a string representation of the object.

Some of the methods listed above are native, which means they are implemented in the native code. It is typically written in C or C++. Native methods are usually used to interface with system calls or libraries written in other programming languages.

In the following topics, we will consider these class methods in more detail.

# Conclusion

The Object class is a default class in the java.lang package and is a root of inheritance in Java programs. Every instance of any class is a kind of Object so there is no need to explicitly extend it when declaring a class. It provides some common methods to all subclasses, including nine instance methods that are divided into four groups in the present topic for your convenience. Some of these methods are native so you can use them to interface with system calls or other programming language libraries. You will learn about the methods in the next topics.

编辑 (opens new window)
#Java#OOP
上次更新: 2022/09/26, 16:55:15
Theory:Referencing subclass objects
Theory:Objects

← Theory:Referencing subclass objects Theory:Objects→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式