Click here to Skip to main content
15,120,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written 2 simple programs in java. These are as follows :

C#
public class simple{
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}



C#
import javax.swing.JOptionPane;
public class simple1 extends JOptionPane{
    public static void main(String[] args){
        JOptionPane.showMessageDialog(null,"Hello World");
    }
}



Yes . these are working just fine with the appropriate output. Now,

when I execute the following statements in my command prompt(to get the content of class file),

C#
javap simple
Compiled from "simple.java"
public class simple extends java.lang.Object{
    public simple();
    public static void main(java.lang.String[]);
}


C#
javap simple1
Compiled from "simple1.java"
public class simple1 extends javax.swing.JOptionPane{
    public simple1();
    public static void main(java.lang.String[]);
}


Now these are the things which I don't understand:

I) Books (Deitel and Deitel) say that java.lang.Object is automatically inherited in EVERY program , but the second class file doesn't show it? If it does then java should support multiple inheritance. There is some kind of a hidden mechanism. What is it?

II) What is the parent class in java?
Posted

1 solution

Quote:
Books (Deitel and Deitel) say that java.lang.Object is automatically inherited in EVERY program
I would say: "every class inherits from java.lang.Object". That's true.

Quote:
but the second class file doesn't show it?
Because it shows just the immediate super class of simple1. The latter inherits from java.lang.Object via JOptionPane (and probably other intermediate classes, that is you have an inheritance chain). This mechanism is NOT multiple inheritance: you have multiple inheritance when a class directly inherits from two (or more) super classes.


Quote:
What is the parent class in java?
The parent class is the direct ancestor (in the inheritance chain) of a class (super provides a way for accessing it, in Java).
   
Comments
SubhamSoni123 7-Nov-13 10:31am
   
But java.lang.Object and javax.swing.JOptionPane are two totally different classes , how can they come under the single inheritance hierarchy???????
CPallini 7-Nov-13 15:04pm
   
Here you may find the inheritance chain from java.lang.Object to javax.swing.JOptionPane:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
SubhamSoni123 7-Nov-13 22:45pm
   
Now I clearly understand this.My final question is , why should the java.lang.Object class be inherited at all(in the first program)?Can't a program work without that?
CPallini 8-Nov-13 3:20am
   
Yes it could be work without. However that's an important feature of Java, see
http://en.wikipedia.org/wiki/Singly_rooted_hierarchy
http://en.wikipedia.org/wiki/Top_type
Form practical point of view, that means you may cast every instance variable to it and you could invoke the java.lang.Object methods, see:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html
on every instance variable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900