Calling methods from other classes
Hi everyone, this is such a dumb question but I'm doing my best in trying to learn java from a book :P
I'm having a hard time with abstraction/modularization, more specifically I want to call on a method in another class.
Im just messing around here but I have made three classes: Car, Wheels, Shell.
Car will abstract from Wheels and Shell.
Now I'm having a compiler error with Car as it does not like the method:
Code :
public int shellSize()
{
//return the size of the shell from the object big
return big.getSize;
}
I have tried everything I know to try and figure out why it won't work - turns out I'm stuck and wondered if you could help?
What I have:
Code :
public class Car
{
private Shell big;
private Wheels blackTyre;
/**
* Constructor for objects of class Car
*/
public Car(String shellColour, int shellSize, String wheelColour, String wheelShape, int howMany)
{
big = new Shell(shellColour, shellSize);
blackTyre = new Wheels(wheelColour, wheelShape, howMany);
}
/**
* Method - Return the size of the shell
*/
public int shellSize()
{
//return the size of the shell from the object big
return big.getSize;
}
}
and then my Shell class:
Code :
public class Shell
{
private String colour;
private int size;
/**
* Constructor
*/
public Shell(String whatColour, int whatSize)
{
colour = whatColour;
size = whatSize;
}
public String getColour()
{
return colour;
}
public int getSize()
{
return size;
}
public void getInfo()
{
System.out.println(" The colour of the shell is: " +colour+ " and the size is: " +size);
}
}
Thanks in advance :o
Re: Calling methods from other classes
Quote:
having a compiler error with Car as it does not like the method
Please copy and paste here the full text of the error message(s).
Re: Calling methods from other classes
The error message I see is:
Quote:
cannot find symbol - variable getSize
So then I tried
Code :
public int shellSize()
{
//return the size of the shell from the object big
return big.size;
}
And I get the error message:
Quote:
size has private access in shell
And thats where I'm at :/
Re: Calling methods from other classes
Quote:
cannot find symbol - variable getSize
Look at this error message again. The compiler is looking for a missing variable
What does the Shell class have that is named getSize?
A variable or a method?
You reference a method by adding an ending ()
Re: Calling methods from other classes
You are incredible, how did I miss that!
thanks!
code:
Code :
public int shellSize()
{
//return the size of the shell from the object big
return big.getSize();
}
Thanks again!
Re: Calling methods from other classes
It pays to take a close look at the error message and try to figure out what the compiler is looking for.