• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Inheritance Error  RSS feed

 
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why am I getting this error I have no idea?
constructor Book in class Book cannot be applied to given types

Book


TravelGuide



Error
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since TravelGuide extends Book and you provide a constructor for it, an implicit call to super() will be made when the TravelGuide constructor is executed.  Your Book class, however, has an explicit constructor that takes three parameters. When you provide a constructor, the compiler will not provide a default no-arguments constructor.  So, the implicit call to super() in TravelGuide will fail with a compiler exception.
 
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below are three changes:
1. Book - Change private to protected as private variables are not accessible to inherited classes
2. Set the values in the Book constructor
3. TravelGuide - Invoke super() in constructor of TravelGuide to invoke base class Book constructor with parameters

Book


TravelGuide

 
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Recaip,the constructor for the TravelGuide class is interpreted as given code by compiler-
but your super class doesn't have no-arg constructor so it cannot be applied in the subclass,two solutions are possible either you provide a no-arg constructor in super class or call super(String,String,String) in the sub-class.

but why have you provide 3 arg constructor in super class,while you do nothing with the arguments,it would be better to replace it by no-arg constructor.

Hope this will help!

kind regards,
praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and one more thing recaip,if you do a no-arg replacement for constructor,then you need to increase the accessibility of the modifier of the class variable as kuldip has mentioned.
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Hi Recaip,the constructor for the TravelGuide class is interpreted as given code by compiler-
but your super class doesn't have no-arg constructor so it cannot be applied in the subclass,two solutions are possible either you provide a no-arg constructor in super class or call super(String,String,String) in the sub-class.

but why have you provide 3 arg constructor in super class,while you do nothing with the arguments,it would be better to replace it by no-arg constructor.

Hope this will help!

kind regards,
praveen.


I just followed instructions of the teacher and got this error. super(String,String,String) doesn't work either...

Also, it seems I cannot keep instances in book to be private since the other class cannot touch them

Here are the steps:
Create a class named Book that has the following:
1. A private String instance variable named isbn.
2. A private String instance variable named author.
3. A private String instance variable named title.
4. A constructor that takes 3 parameters in the order of isbn, author, and title.
5. A method named printInfo that does not take any parameters and does not return
anything. This method should print out the value of each instance variable on its own
line.
6. Compile the Book class.
ˆ Create a class named TravelGuide that inherits from Book and that has the following:
1. A private String instance variable named country.
2. A constructor that takes 4 parameters (with country being last) and sets the instance
variables of the class and the superclass.
3. A method named printDetail that does not take any parameters and does not return
anything. This method should print out the value of the instance variable on its own
line.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:
I just followed instructions of the teacher and got this error. super(String,String,String) doesn't work either...


Question. Did you call your super constructor with 3 required strings (isbn, author, and title)? If so, please show us the subclass code.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:
Also, it seems I cannot keep instances in book to be private since the other class cannot touch them


And if you followed your instructors code correctly, your subclass would not need to "touch" the private variables of the superclass.

Henry
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Recaip Sanli wrote:
I just followed instructions of the teacher and got this error. super(String,String,String) doesn't work either...


Question. Did you call your super constructor with 3 required strings (isbn, author, and title)? If so, please show us the subclass code.

Henry


  
   public TravelGuide(String isbn, String author, String title, String country){
         super(isbn,author,title);
         this.country = country;
      }
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the error you got? That doesn't look like it would run fine.  If it doesn't work, what does it do and what are you expecting it to do?
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:What is the error you got? That doesn't look like it would run fine.  If it doesn't work, what does it do and what are you expecting it to do?


This is the test file


This is the error



This is the result I am expecting
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really need to access the private fields of the superclass? Try commenting out the call to tg.printDetails() and see what you get when you just call tg.printInfo(). Then, see what modifications you need to make so that tg.printDetails() gives you the rest of the output that you need.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:
I just followed instructions of the teacher and got this error. super(String,String,String) doesn't work either...

Also, it seems I cannot keep instances in book to be private since the other class cannot touch them
no you can keep instances to be private in Book,in accordance with your steps you need to declare 3-arg constructor(with public,protected or default access)
and set the variables in there,afterwards during subclassing call this 3 arg constructor at very first line in the constructor body as given example below:-
but don't try to access(set) the super class variable in your sub-class or you will get the compile time error like the given code below:-

as concerned with your requirements(steps),you really don't need to change the private access of instance variable in your super class,simply follow the procedure as mentioned above(in the first code).

kind regards,
praveen.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only just looked at this thread.
The Java® way to do this would be to override toString() to return what you want, then printInfo can look like this:-Whenever println encounters a non‑null object, it calls toString on it (indirectly), so that would behave as if you had written
System.out.println(toString());
which is less easy to read.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . you can keep instances to be private in Book . . . .
That is the whole idea of inheritance; a travel guide IS‑A book, so those parts of the travel guide object which are also parts of the book object remain unchanged. You start off creating the travel guide object (run constructor) by creating that part of it which is the book object (that is why the super(...); call goes first), then you add those parts which are specific to the travel guide. You don't attempt to repeat parts of book in travel guide. Only those parts which are new to it, which includes all the constructors because constructors aren't inherited. You can even do that with toString:-There is now no need to do anything with printInfo in travel guide.

edit: add alternative format for toString which obviates need for LINE_END constant with %n tag:-
 
Kuldip Shetty
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By adding public getter and setter method for instance variables in Book you should be able to print the values of the private variables of Book in TravelGuide

Book



TravelGuide
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kuldip Shetty wrote:By adding public getter and setter method for instance variables in Book you should be able to print the values of the private variables of Book in TravelGuide
...

That is still not a good solution. The idiomatic (and IMO best) solution is the one suggested by Campbell.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kuldip Shetty wrote:By adding public getter and setter method for instance variables in Book . . .
As Junilu said, that solution fails to use the power of objects; you already have a method to print the details in the book class, so the travel guide class only has to enhnce it slightly. It would not be as  good to have two methods which print the author like that. Another thing sis that public get and set methods can jeopardise encapsluation.
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you @praveen kumaar, but the below code didn't work..


Thank you @Campbell Ritchie, below is still giving an error...


Thank you @Kuldip Shetty, but there's no step to code getters and setters so I believe my instructor don't want me to create them.

To all of you guys, it amazes me how much you know about Java. I am a very beginner so I am really trying hard to understand causes of errors here and how to overcome them. Thank you for all deep thoughts!

Also, I am deeply shocked how knowledge coders like you guys cannot come up with an idea what my instructor wants. It definitely makes a difference to have a good instructor, I guess mine isn't helpful at describing steps as well as helping us to understand concepts.

Below is the latest code I have and I still get error. I am totally lost.

Book


TravelGuide


Error

 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is Test main file


Here is steps she wrote to follow
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:Thank you @praveen kumaar, but the below code didn't work..



Yet... it worked previously. 

Perhaps it would be a good idea to take a look at your previous constructor code versus your new constructor code, and noticed the difference?

[EDIT: And yes, I agree. Praveen's code does not compile. You may need to take the code examples as pseudo code.]

Henry
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you see the problem with this line?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi racaip!,in my previous post i had written one wrong line (by mistake,sorry for that)-please do a correction as-


kind regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . mistake,sorry for that . . .
We all make mistakes and it is a good sign when somebody acknowledges their mistake and apologises
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:. . . Thank you @Campbell Ritchie
That's a pleasure
below is still giving an error...
The bit of code I posted is correct; please tell us the full details of the error, otherwise we can't help.
. . . there's no step to code getters and setters so I believe my instructor don't want me to create them.
I personally would think that code which requires getXXX or setXXX methods shou‍ld have such methods whether you are told to or not. You shou‍ld be able to use your initiative to add any code necessary.
But I believe you can write the printInfo method and toString without those getXXX methods.
To all of you guys, it amazes me how much you know about Java. . . . Thank you . . . . . .
That is because we have years of practice and have made all the same sort of mistakes ourselves. And to repeat what I said earlier:
That's a pleasure
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
She said
Your TravelGuide class should not be doing anything with the superclass instance variables (which should be private per the instructions) other than setting them in the constructor using super. TravelGuide should be printing out the value of its own instance variable, but not the values of the superclass instance variables.


So since TravelGuite extends Book, it is getting all the instance variables that Book has. Where I am getting lost is when I try to print with this.isbn for example, it gives and error because that instance variable is private in Book class.
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lastly, Thank you everyone. I solved the great mystery..

Before


After


Outcome from this though, if you extend a class, you cannot access parent class's instance variables directly. You have to use a method within the parent class to be able to touch parent class's values.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:
So since TravelGuite extends Book, it is getting all the instance variables that Book has. Where I am getting lost is when I try to print with this.isbn for example, it gives and error because that instance variable is private in Book class.


As already mentioned (or hinted many times), you do not need to change the Book class to implement the TravelGuide print method. Anyway, no more hints ...

Your TravelGuide printing method does *not* need to access the private variables to print the results. If you look at the Book class, it already has a printing method. So, your TravelGuide printing method just needs to call the Book printing method, and then, print an extra line. You get the desired result, and the TravelGuide printing method doesn't need to access the Book private variables.

[EDIT: Wow. You got the answer a minute earlier than us directly giving you the answer. Good job.]

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:Outcome from this though, if you extend a class, you cannot access parent class's instance variables directly. You have to use a method within the parent class to be able to touch parent class's values.

This is true ONLY if the variables in the superclass have been declared as private. Anything else that isn't private is accessible with the exception of one case with package private (default) scoped variables.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:She said
Your TravelGuide class should not be doing anything with the superclass instance variables (which should be private per the instructions) other than setting them in the constructor using super. TravelGuide should be printing out the value of its own instance variable, but not the values of the superclass instance variables.
. . .
Agree. That is the whole idea behind inheritance.

Don't put anything which doesn't require formatting inside code tags. Code or formatted output is all right, but code tags aren't suitable for ordinary text. In this case it produced lines much too long and I had to change it.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recaip Sanli wrote:. . . 3. A method named printDetail that does not take any parameters and does not return anything. This method should print out the value of the instance variable on its own line.
Is that exactly what you have been told to do in the travel guide class? Have you been given any more instructions about the printInfo method?
 
Recaip Sanli
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that exactly what you have been told to do in the travel guide class?
Yes.

Have you been given any more instructions about the printInfo method?
No, I have not.


It makes sense though...
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, you've got the solution
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!