abalfazl hossein wrote:1-Generally,When a class is subclass of another, Is it necessary to call superclass constructor?Or It is only necessary When You need use variable of Superclass?
2-In above code If I Use i, That is an int, an error of compiler will show, But If I use for example 4, It works well!
Why?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
Because i is not initialized yet, and has no value (not even 0). When your constructor is called this is the order of events
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
1) the super class constructor is called; i is not initialized yet
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at superclass.Subclass.<init>(Subclass.java:9)
at superclass.Main.main(Main.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at superclass.Subclass.<init>(Subclass.java:9)
at superclass.Main.main(Main.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at superclass.Subclass.<init>(Subclass.java:9)
at superclass.Main.main(Main.java:15)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Campbell Ritchie wrote:If you have compilation problems, don't run it. Simply compile the code and read the errors.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
init:
deps-jar:
Compiling 1 source file to C:\Users\Yazra\Documents\NetBeansProjects\Superclass\build\classes
compile-single:
BUILD SUCCESSFUL (total time: 0 seconds)
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Line 4 is actually called before line 2. That's why you can't use i on line 4.
abalfazl hossein wrote:Sir may you explain more details?outdated files?
The result is:
At first variables of super class must be initialized, Then Subclass. Right?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions