• Post Reply Bookmark Topic Watch Topic
  • New Topic

JQPlus---constructor  RSS feed

 
Ash sav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Sub extends Super{
int i,j,k;
public Sub(int m, int n) {i =m; j=m;}
public Sub( int m) {super(m); }
}
Which of following constructor must exist in Super class for sub class to compie correctly.
a. public Super(int a, int b)
b. public super(int a)
c public Super()
d. only Super(int a) is required
According to me answer is b and d but acc to JQPlus answer is b and c I don't understand why default constructor is needed? Please give me some explanation.
Thanks in advance,
Ash
 
Elizabeth Jacob
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Sub extends Super{
int i,j,k;
public Sub(int m, int n) {
i =m; j=m;}
public Sub( int m) {
super(m); }
}
RHE says "the default constructor is not inherited;it is created
by the compiler if and only if you dont provide any other constructors in the source of the particular class."
whenever a constructor without the 'this' or 'super'keyword is
encountered the compiler adds an implicit call to super().so in
the super class if you are providing just one constructor as
super(n) then the compiler wont create the default constructor and it causes compiler error
so when this constructor
public Sub(int m, int n) {
i =m; j=m;}
is encountered the compiler has to create a default constructor of its super class.since there is an explicit call to
super(m) in the above example, you need to have Super() in the
Super class.
hope this helps,
Elizabeth.

 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Sub extends Super{
// u r extending super so everytime u make an object of sub, the constructor of super will be called before the sub constructor can be executed.
int i,j,k;
public Sub(int m, int n) {i =m; j=m;}
// here u will create an object as Sub = new Sub(10,20)
but the super's constructor has to be called before sub constructor
u haven't specified in the constructor how to call super so implicitly a call will be placed to the default constructor of super hence a default constructor should exist in super

public Sub( int m) {super(m); }
// when u make an object here as Sub s = new Sub(10)
u r specifying teh call to super constructor with super(m) which is super(10)
and hence the super class should have super(int i) constructor
}
Which of following constructor must exist in Super class for sub class to compie correctly.
a. public Super(int a, int b)
b. public super(int a)
c public Super()
d. only Super(int a) is required
According to me answer is b and d but acc to JQPlus answer is b and c I don't understand why default constructor is needed? Please give me some explanation.
Thanks in advance,
Ash[/B]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!