I wonder what is the difference between my own object type and...
Hi all,
I have a class/interface called A, now I have seen that I can declare a method of type my class/interface A. But as I am very basic in java I do not understand what is the benefits of doing that. Instead of just declaring my methods of types such as String void etc.
I hope you understand what I mean
I appreciate your help
kkalar
[384 byte] By [
sojag] at [2007-9-26 1:38:50]

Hi
> I have a class interface called A. And you have said >that you can declared a method of type my > class/Interface A.
Here I am not that much clear.
For eg after creating a class called
class CharStack
This Charstack is used for declaring a reference variable of the appropriate class to store the reference to the object.
CharStack stack1, stack2;
And the same thing can be used for creating an object.
stack1 = new CharStack(10);
This new operator returns a reference to the charStack class.
But in your query, you are talking some thing about methods in using the class what we have created.
Also please see this URL.
http://java.sun.com/docs/books/tutorial/java/javaOO/index.html
Thanks
Bakrudeen
Thanks for that My question was if we declare a method of type class A what are we trying to get from class A then?Thanks for your help againSojag
sojag at 2007-6-29 2:27:16 >

? What ever the instances of the class A can give us?
Hi,It will create a new instance of the class.ThanksBakrudeen
Your class, Bicylce, is an object. From that object you want get/change information. So yoy create methods like setSpeed, getSpeed, getNumberOfWheels, setRider, getRider etc.So Bicycle myBicycle = new Bicycle()myBicycle.setSpeed(10)etc.Klint
saen at 2007-6-29 2:27:16 >

Lets say you have a class Game
and a class Player
when you create a new Game
Game myGame = new Game()
the code inside the constructor of Game might create
two instances of the class Player
now if you want to get Information about the players,
or set Information of them you need access to them.
For this purpose your Game class could implement
a method like this:
public Player getPlayer(int index){
return playerArray[index-1];
}
assuming playerArray is an array holding the players.
Follow the link to the java tutorial posted by somebody
I'm sure it will help
Spieler
Thanks a lot Speiler.So Jag
sojag at 2007-6-29 2:27:16 >

And thanks to all too
sojag at 2007-6-29 2:27:16 >

You must be talking about constructors.
The benifits of using a constructor, instead of assignment methods is that you can quickly setup all the fields of your object, when you are creating the object. You can also have default values and create generic classes, by using a parameterless constructor which will use default values.
ie.
//Proper implementation
A a = new A("Stuff",10,true);
class A{
//generic constructor
A(){
this.string = "Generic";
this.integer = 100;
this.print = true;
}
//other functionality
boolean canPrint(){
return this.print;
}
private boolean print;
private int integer;
private String string;
}
In this way you don't have to call a method aswell like
//Bad implementation
A a = new A();
a.setup("Tedious",10,true);
class A{
void setup(String string,int integer,boolean print){
this.string = string;
this.integer = integer;
this.print = print;
}
//other functionality
boolean canPrint(){
return this.print;
}
private boolean print;
private int integer;
private String string;
}
Finally
A a = new A();
if(a.canPrint()){
//value
}
In the first implementation this will use the default values you specified. In the second the default will always be true!
Hope that helped.
