• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help on type mismatch  RSS feed

 
Rayt Leeop
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all,

I initialized an array in the subclass like this:

public Sedan(){
Vehicle obj=new Vehicle();

System.out.println("the Sedan constructor is starting...");
doors=new Door[4];
doors[0]="fLeft";
doors[1]="fright";
doors[2]="RLeft";
doors[3]="RLeft";
But the compiler said "cannot convert String to Door".

Door[] is declared in supercalss like this:

private Door[] doors=new Door[2];


Any helps or suggestions please?
 
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
Rayt Leeop wrote:
doors=new Door[4];
doors[0]="fLeft";
doors[1]="fright";
doors[2]="RLeft";
doors[3]="RLeft";
But the compiler said "cannot convert String to Door".


The compiler error kinda says it all. A Door array hold Door instances. And a String is not a Door. Perhaps the Door class has a constructor that takes a string, and you meant to instantiate a Door instance?

Henry
 
Rayt Leeop
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Henry, Thanks for your reply. I have superclass in which the Door[] array is created like this:

private Door[] doors=new Door[4];//create uninitialized array


I have Door class which is composite to the Car class. The Door class is:

package lightweight;

public class Door {

public Window window;

public Door() {
window = new Window();
}

public void open() {
System.out.println("Door is open");
}
public void close() {
System.out.println("Door is closed");
}
}


Any suggestions?
 
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
Rayt Leeop wrote:Hi, Henry, Thanks for your reply. I have superclass in which the Door[] array is created like this:

Any suggestions?


Not sure what you are trying to achieve. You can't assign a String instance into an array that hold Door objects -- and showing us how you instantiated the door array or the code for the Door class doesn't change that.

Perhaps you can elaborate what you are trying to do?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!