0

So say I have 3 classes, Tester, Fruit (superclass), and Apple (subclass)

I have written a new method in Apple (which extends Fruit). The method being:

public String getAppleColor()

Now say in Tester I have created an array of 10 Fruit

Fruit fruitArray = new Fruit[10]

and say I make one of them

fruitArray[3] = new Apple()

This is fine, since Apple is also of type Fruit. However I wish to use my getAppleColor() on this specific element of the array:

String appleColor = fruitArray[3].getAppleColor();

How come this is not working? When I look at useable methods on fruitArray[3] in eclipse, none of my Apple methods show up, but I made fruitArray[3] an Apple?

user3029760
  • 165
  • 4
  • 7
  • It's because Java is a statically typed language. You may be used to dynamically typed languages like most scripting languages. You can only invoke methods that are known at compile time to be part of the type. You can use a cast expression to override the static type of a variable. (For a *solution* to your problem, see the answers below) For more information: http://stackoverflow.com/questions/125367/dynamic-type-languages-versus-static-type-languages – Erwin Bolwidt Jan 25 '14 at 04:32
  • If you already know that `fruitArray[3]` is an apple, don't you also already know the color? In other words, why does it make sense to assume `fruitArray[3]` is an apple, but it doesn't make sense to replace the call to `fruitArray[3].getAppleColor()` with `"Red"` or `"Green"` or something? – DaoWen Jan 25 '14 at 04:50

4 Answers4

2

You can't call getAppleColor() on Fruit reference, it doesn't declare that method

better design would be have getFruitColor() defined and make Fruit abstract class / make it interface and force each Fruit implement this method

Compiler doesn't know what implementation it is going to be assigned at runtime

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thanks. Just for confirmation, if I create an object (Fruit object in this case) and want to use a subclass method, it is mandatory that I have that method implemented in the superclass too (Fruit)? – user3029760 Jan 25 '14 at 04:58
  • No declaration is fine, please refer to polymorphism in java – jmj Jan 25 '14 at 05:06
2

You have to cast it to an Apple since the compiler won't know that fruitArray[3] will contain an Apple (it might contain any other sort of Fruit). Try:

String appleColor = ((Apple)fruitArray[3]).getAppleColor();
Lord M-Cube
  • 361
  • 3
  • 11
1

The compiler cannot know that at runtime, an element declared as a Fruit is actually an Apple. As such, it won't let you call any methods declared in Apple.

Your array is

Fruit[] fruitArray; 

The compiler can only know that the elements in the array are Fruit instances, nothing more.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

You'd better declare a method getColor() in class Fruit, override it in subclass Apple, then you can get color of apple by fruitArray[3].getColor().

DaoWen
  • 32,589
  • 6
  • 74
  • 101
mojiayi
  • 187
  • 1
  • 2
  • 11