File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Inheritance, Constructor, Super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance, Constructor, Super" Watch "Inheritance, Constructor, Super" New topic
Author

Inheritance, Constructor, Super

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635



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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

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?

The first call of any constructor is either a call to a constructor of the super class (using super) or to another constructor of the same class (using this). If you omit this call the compiler will add "super();". So if your super class has a constructor that doesn't take any arguments then you don't need to call the super class constructor explicitly; it will be called implicitly instead. If the super class does not have this constructor then you must specify which constructor to use, and supply the arguments.

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?

Because i is not initialized yet, and has no value (not even 0). When your constructor is called this is the order of events:
1) the super class constructor is called; i is not initialized yet.
2) i is initialized to 4.
3) the rest of your constructor runs.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

1) In your subclass constructor, JVM adds the super() automaticaly note that this call only for default constructor if you have defined your own non default constructor(which takes parameters) then you should explictly call your super calss constructor.

2)Intance variables get their default values after completion of executing the super class constructor. You can't use instace varialbe until the completion of super class constructor.

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


This code does not work.The problem is about constructor in subclass.Why?When A parent's constructor has not argument:

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.


http://download.oracle.com/javase/tutorial/java/IandI/super.html
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Because i is not initialized yet, and has no value (not even 0). When your constructor is called this is the order of events



int i=4

It is initialized !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

But only after the call to super(). I clearly explained the exact order of events in that example.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
1) the super class constructor is called; i is not initialized yet


In which line of this code super constructor?1 or 3?




This code does not work.The problem is about constructor in subclass.Why?When A parent's constructor has not argument

May someone answer my last question?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Line 4 is actually called before line 2. That's why you can't use i on line 4.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


This code does not work.The problem is about constructor in subclass.Why?

When A parent's constructor has not argument:

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

ItDoesntWorkIsUseless. It compiles, at least on my system. So what's the problem?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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)


problem is about this line:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

And you're certain you've still commented out lines 15 and 16? Because if both are commented out line 16 must be the first statement in the constructor, and therefore must come before line 15.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
You ran this code and haven't any problem?!!! on line 9:



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)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

And what errors does the compiler give you?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
If you have compilation problems, don't run it. Simply compile the code and read the errors.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I want to know why this code throws this exception:

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)


I use netbeans

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
We know you use NetBeans; we can tell from the error message. Compile the thing and tell us what the error message is, and which line it is on.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Campbell Ritchie wrote:If you have compilation problems, don't run it. Simply compile the code and read the errors.

I agree. Never run applications while there is still at least one compiler error.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
init:
deps-jar:
Compiling 1 source file to C:\Users\Yazra\Documents\NetBeansProjects\Superclass\build\classes
compile-single:
BUILD SUCCESSFUL (total time: 0 seconds)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
This is the netbeans that I see:

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


But constructor in Superclass matches, Why this exception throws?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Your class file is probably outdated. Try to do a full rebuild of the entire project.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
It works!

Sir may you explain more details?outdated files?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
I trace the first code, and, I see you are right:


Line 4 is actually called before line 2. That's why you can't use i on line 4.





The result is:

At first variables of super class must be initialized, Then Subclass. Right?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

abalfazl hossein wrote:Sir may you explain more details?outdated files?

For some reason NetBeans got confused and didn't compile all the files that it should have compiled. You can fix this by cleaning all the compiled *.class files and rebuilding everything from scratch ("rebuild all"). This normally shouldn't happen, and it's hard to say after the fact why you had this problem.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Thank you very much Jesper and other friends who help me.

The result is:

At first variables of super class must be initialized, Then Subclass. Right?


Please confirm.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Right. But not only variables, the entire super class constructor must end before variables of the sub class are initialized.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance, Constructor, Super