• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Use super() to override an override -> error : cannot resolve type

 
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am trying to use super() to override local constructors and methods.
I have a package with 3 class files:
* TestName.java where AName object is created
* TestPerson.java which uses AName to construct a composite object APerson
* AStudent.java which extends TestPerson and then overrides the inherited APerson's name and sex with local values.

Eclipse threw the errors below, in AStudent...

I cannot see where I went wrong at the moment, a bit of help would be appreciated...




TestPerson.java follows
// ==============


So far so good, APerson runs and inherits from AName.

Now I add a 3rd level, AStudent, which extends TestPerson, and then use super() to override the inherited values with local values for AName...


// AStudent.java
// =============

 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AStudent(Name name, char sex, int credits, double gpa) { // ERROR : name cannot be resolved to a type


Where is class Name defined?
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

AStudent(Name name, char sex, int credits, double gpa) {
// ERROR : name cannot be resolved to a type


Where is class Name defined?



Answer: in constructor AStudent(AName name, ...), see modified codes below (I had multiple errors in there, but even after refactoring, I am still not overriding the local override).


 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

AStudent(Name name, char sex, int credits, double gpa) { // ERROR : name cannot be resolved to a type


Where is class Name defined?



A tidy version of AStudent.java, after refactoring, Eclipse shows no errors, but when I try to run it, it shows results from TestPerson and not from AStudent:

 
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AhFai Chan wrote:. . . it shows results from TestPerson and not from AStudent:

That is normal. You have instructed it to use the superclass version and it does so.

Don't try putting any other tags inside code tags; it makes the post hard to read and impossible to compile.
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

AhFai Chan wrote:. . . it shows results from TestPerson and not from AStudent:

That is normal. You have instructed it to use the superclass version and it does so.

Don't try putting any other tags inside code tags; it makes the post hard to read and impossible to compile.



I am not understanding this right!

// the final result should like like this
ann: Anne Baker (sex F)
credits: 16
gpa: 3.5


No eureka moment for me... yet
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't AStudent extend APerson, not TestPerson?
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit curious as to what you mean by this statement :

AhFai Chan wrote:Hi, I am trying to use super() to override local constructors and methods.



You cannot override a constructor, they are overloaded. You can override a method, but not with using super keyword.
 
Campbell Ritchie
Marshal
Posts: 76872
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason Bullers is right; you have extended the wrong class.
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Bullers wrote:Shouldn't AStudent extend APerson, not TestPerson?



Yes, Jason, extending APerson solved the problem. I did extend the wrong class.

But Eclipse thew error, I had to split out class AStudent into a seperate class-file AStudent.java to resolve it. WHY though, doesn't make sense to me?

I have not expected this behavior, I do not know whether Eclipse has gone idiosyncratic on me, or Java not behaving the way I had thought it would.

Here are the two files, they work now, yielding the expected results:
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:I am a bit curious as to what you mean by this statement :

AhFai Chan wrote:Hi, I am trying to use super() to override local constructors and methods.



You cannot override a constructor, they are overloaded. You can override a method, but not with using super keyword.





These are the overloaded constructors in TestPerson.java:


 
Jason Bullers
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AhFai Chan wrote:But Eclipse thew error, I had to split out class AStudent into a seperate class-file AStudent.java to resolve it. WHY though, doesn't make sense to me?



What was the error?
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Bullers wrote:

AhFai Chan wrote:But Eclipse thew error, I had to split out class AStudent into a seperate class-file AStudent.java to resolve it. WHY though, doesn't make sense to me?



What was the error?



That I have to split them into two separate class files, won't compile otherwise. Worked well after that.

Why would I want to split them into two class files? It doesn't make sense to me.
 
Jason Bullers
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AhFai Chan wrote:That I have to split them into two separate class files, won't compile otherwise. Worked well after that.

Why would I want to split them into two class files? It doesn't make sense to me.



I'm guessing that you had more than one public class in the same source file. Java only allows you to have multiple classes in the same source file if at most one of them is public, and the one that is public must match the name of the file.
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Bullers wrote:

AhFai Chan wrote:That I have to split them into two separate class files, won't compile otherwise. Worked well after that.

Why would I want to split them into two class files? It doesn't make sense to me.



I'm guessing that you had more than one public class in the same source file. Java only allows you to have multiple classes in the same source file if at most one of them is public, and the one that is public must match the name of the file.



Jason, you are right again

Which means I have to be careful when I take the Eclipse defaults while clicking through the create process. In this instance, I think I simply checked the box for inheriting from superclass...

Thanks for your input.
 
I just had the craziest dream. This tiny ad was in it.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic