• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
// =============

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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:

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • 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: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't AStudent extend APerson, not TestPerson?
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers is right; you have extended the wrong class.
 
AhFai Chan
Ranch Hand
Posts: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 81
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!