Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Does java extend 2 classes at one time

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Does java extend 2 classes at one time

    1) when you create a class and compile it and open and see the class using javap command .. that class file contains that it extends java.lang.object and a default constructor is created automatically..... But when you extends another class the java.lang.Object class is not appeared as extended why?

    Code example:

    1St Case ----- Java code:

    public class temp(){

    }

    Javap Code:

    public class temp() extends java.lang.Object {
    public temp(){
    }

    }


    2nd case --- Java Code:

    public class temp() extends dummy{

    }

    javap Code:

    public class temp() extends dummy{
    public temp(){
    }

    }
    why for the above scenario it doesn't extends object class .. if it does implicitly then why it did not do in the first case instead why did the compiler extends Object class ?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,400
    My Mood
    Mellow
    Thanks
    143
    Thanked 600 Times in 587 Posts

    Default Re: Does java extend 2 classes at one time

    Cross posted here.

    Welcome! Please read this topic to learn how to post code correctly and other useful info for new members.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Does java extend 2 classes at one time

    Quote Originally Posted by raghz View Post
    why for the above scenario it doesn't extends object class .. if it does implicitly then why it did not do in the first case instead why did the compiler extends Object class ?
    Please, try this code to understand.

    HierarchyTest.java
    public class HierarchyTest {
        public static void main(String[] args) {
            printHierarchy(test1.class);
            printHierarchy(test2.class);
            printHierarchy(test3.class);
        }
     
        private static void printHierarchy(Class c) {
            System.out.print(c.getName());
            c = c.getSuperclass();
     
            while (c != null) {
                System.out.print(" --> ");
                System.out.print(c.getName());
                c = c.getSuperclass();
            }
     
            System.out.println();
        }
    }
     
    class test1 { }
     
    class test2 extends test1 { }
     
    class test3 extends test2 { }
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does java extend 2 classes at one time

    So if i understand correctly if you extend any class, the parent class will be extending java.lang.object class.

    So by default java always extends one class at least every time. Thanks for the clear explanation

Similar Threads

  1. Using Date() to get Start Time and Finish Time of a copyFiles method
    By dalythe in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 17th, 2013, 09:50 PM
  2. Class loader wont let me extend classes...?
    By sci4me in forum Java Theory & Questions
    Replies: 9
    Last Post: May 19th, 2013, 12:47 PM
  3. Chrome Extension for Flight Search, how can java help to extend.
    By prshnt in forum Java Theory & Questions
    Replies: 1
    Last Post: August 29th, 2012, 05:11 PM
  4. [SOLVED] how to extend a subclass?!
    By migongotar in forum Object Oriented Programming
    Replies: 4
    Last Post: August 2nd, 2011, 03:22 PM
  5. is there a better way to extend an array?
    By kurt-hardy in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2011, 05:33 PM