wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance Issue" Watch "Inheritance Issue" New topic
Author

Inheritance Issue

Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102


Query:

If i change line 2 to public int a; then shouldn't it work?

Whizlabs
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Why do you think, it wont work??? :?:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

If i change line 2 to public int a; then shouldn't it work?


You don't have to go that far. Changing it to "protected" will allow the subclass to compile. Heck, if both the super and sub are in the same package, then changing it to the default access will work too.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
I changed the code to


It seems there should be a default constructor in class Super or Sub constructor at 1 should be


Compiler must be looking for default Super constructor which is getting implicitly called from fist line of Sub's default
constructor

Regards
Sunny



Regards
Sunny Mattas
SCJP5
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
What happens to the default constructor when you add a non-default constructor to a class?
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
If there is a non default constructor in class then compiler will never put any default constructor by itself.
Programmer himself has to take care that there is no call to default constructors from its subclass.
As implicitly first line in a constructor is super() that is a call to default constructor of super class error may be produced.
So always call the non default constructors of super class yourself from sub class constructor using super(arguments).
One can also call the constructor in same class using this() or this(arguments).

Regards
Sunny
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Thanks Guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance Issue