Results 1 to 4 of 4
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Questions about the Object class

    According to java, "Class Object is the root of the class hierarchy. Every class has Object as a superclass".

    And this is what I understand in inheritance. A class that extends another class is a superclass while a class that is being extended is a subclass.

    So, this is how we write a class

    class ClassA
    {

    }

    class ClassB extends ClassA
    {

    }

    And java says that Object is the superclass of every class. Does that include the classes that we created?
    Because if it does then ClassB has a multiple inheritance because it extends the Object class implicitly and extends
    ClassA explicitly.

    Or, Object class won't subclass ClassB because it already has a superclass which is ClassA?

    Can someone enlighten me with this.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: Questions about the Object class

    All classes implicitly inherit from Object class. If you create your own subclass, that subclass, and all super classes above it inherit from
    the Object class that is at the top of the inheritance hierarchy. No multiple inheritance is involved.

    Regards,
    Jim
    Last edited by jim829; 04-25-2018 at 05:40 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Re: Questions about the Object class

    So, Object class extends every class implicitly. I'm just intrigue on what I see when reading some documentation. For example the StringBuilder class in the documentation

    https://docs.oracle.com/javase/8/doc...ngBuilder.html

    Object class explicitly extends StringBuilder. I'm just wondering why a class like StringBuilder explicity used extends keyword to be extended by Object, isn't redundant?

    If I make my class explicitly extended by Object will that be redundant?

    for example

    class ClassA extends Object{}

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: Questions about the Object class

    Actually, StringBuilder extends Object. But that's in there for continuity to alert folks that StringBuilder sub classes Object. All classes do. But since you can't do multiple inheritance if you extend Object explicitly you won't be able to subclass another class.

    Here is an excerpt from the Java Tutorials.

    Object as a SuperClass

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Object Library Hashmap, Size Questions
    By augustas656 in forum New To Java
    Replies: 1
    Last Post: 07-16-2014, 11:24 PM
  2. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  3. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  4. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  5. Replies: 3
    Last Post: 03-15-2010, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •