Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Issue

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Query:

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

Whizlabs
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think, it wont work??? :?:
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens to the default constructor when you add a non-default constructor to a class?
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic