• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance, Constructor, Super

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But only after the call to super(). I clearly explained the exact order of events in that example.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 4 is actually called before line 2. That's why you can't use i on line 4.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ItDoesntWorkIsUseless. It compiles, at least on my system. So what's the problem?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what errors does the compiler give you?
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have compilation problems, don't run it. Simply compile the code and read the errors.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the netbeans that I see:

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But constructor in Superclass matches, Why this exception throws?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class file is probably outdated. Try to do a full rebuild of the entire project.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works!

Sir may you explain more details?outdated files?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. But not only variables, the entire super class constructor must end before variables of the sub class are initialized.
 
reply
    Bookmark Topic Watch Topic
  • New Topic