Results 1 to 6 of 6
  1. #1
    Ipsita is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Question How to inherit "Object" class in Java

    How "Object" class is inherited by all java class by default though it is not extended?

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Probably JLS says that, and the following code proves the fact.

    Java Code:
    class ParentTest{
    
    	public static void main(String arg[]){
    		ParentTest t=new ParentTest();
    		if(t instanceof Object){
    			System.out.println("Point is true");
    		}
    	}
    }
    Swastik

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Another way to prove the fact

    Java Code:
    class ParentTest{
    	private void showParentClass(){
    		System.out.println("Parent class name is "+getClass().getSuperclass().getName());
    	}
    	public static void main(String arg[]){
    		ParentTest t=new ParentTest();
    		t.showParentClass();
    	}
    Swastik

  4. #4
    Ipsita is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Question

    Yes parent class of any class is Object I know. But I want to know how Object be parent class of all as we don't even extend it?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Ipsita View Post
    Yes parent class of any class is Object I know. But I want to know how Object be parent class of all as we don't even extend it?
    Because that is how Java is defined per the JLS, period (as mentioned already in the first reply to this thread).

    Please see: 4.3.2 The Class Object

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Its tough to say how, but that is what java language specification says.

    Types, Values, and Variables
    Swastik

Similar Threads

  1. lock on ".class" object
    By rajinder5 in forum Threads and Synchronization
    Replies: 0
    Last Post: 10-11-2010, 05:38 PM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  3. Replies: 1
    Last Post: 04-18-2009, 06:14 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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