Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Question

 
Akashh Kumarr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code appearing in Eagle.java
class Bird {
private Bird(){ }
}
class Eagle extends Bird {
public String name;
public Eagle(String name){
this.name = name;
}

public static void main(String[] args) {
System.out.println(new Eagle("Bald Eagle").name);
}
}
What can be done to make this code compile? Select 1 option.

1. Nothing, it will compile as it is.
2. Make Eagle class declaration public: public class Eagle { ... }
3. Make the Eagle constructor private: private Eagle(String name){ ... }
4. Make Bird constructor public: public Bird() { ... }
5. Insert super(); as the first line in Eagle constructor:     
public Eagle(String name){         
super();         
this.name = name;     
}

Also, please give explanation to support your answer.

Is it true that all the user-defined constructors implicitly call the super class constructor even if we don't call it explicitly?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question comes from our question bank ( QID enthuware.ocajp.i.v7.2.907 ) and there is a detailed explanation with it:

If a subclass class constructor doesn't explicitly call the super class constructor, the compiler automatically inserts super(); as the first statement of the subclass constructor.

Since the constructor of Bird is private, the subclass cannot access it and therefore, it needs to be made public. protected or default access is also valid.


Please do let us know which part is not clear so that we can improve it. You can also click on "Discuss" button at the bottom of question display screen and post a follow up question directly to us.

thank you,
Paul.
discuss.png
[Thumbnail for discuss.png]
 
Akashh Kumarr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:This question comes from our question bank ( QID enthuware.ocajp.i.v7.2.907 ) and there is a detailed explanation with it:

If a subclass class constructor doesn't explicitly call the super class constructor, the compiler automatically inserts super(); as the first statement of the subclass constructor.

Since the constructor of Bird is private, the subclass cannot access it and therefore, it needs to be made public. protected or default access is also valid.


Please do let us know which part is not clear so that we can improve it. You can also click on "Discuss" button at the bottom of question display screen and post a follow up question directly to us.

thank you,
Paul.


Thanks a lot for clearing up. Next time, I'll use the Discuss feature.
I only knew that default constructor has implicit call to super class constructor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic