0

A simple question on Java class

I want to create my class with my own add method which adds an object of a different class.

For eg if my class name is Weapon, I want to create an add method like void add(Gun gun) (Gun being one of my other classes for example)

This would be just like creating your own collection maybe??? if I am not wrong? Like list.add() method, I want to implement my own add method for the Weapon class

So when I instantiate an object of my weapon class, it should be like Weapon w = new Weapon(new Gun( ... .. constructor parameters) )

Also I would like to have variants of my add methods. Like a different add method with different parameters like void add(Weapon weapon) ( creating an method to add your own class object )

So I am not sure if this is similar to implementing a collection?? And if it is how do I achieve it? Do I have to extend another class for this?

Apologies if my question confused anyone! :)

5
  • definitely confused me. Commented Feb 21, 2014 at 0:47
  • You are right, it does confuse me. It is unclear what you are trying to achieve and what your actual problem is. If you want an add() method, what stops you from writing one? What does it has to do with collections? Commented Feb 21, 2014 at 0:47
  • Just to point out the obvious: Weapon w = new Weapon(new Gun(...)) doesn't really make sense to me. On the other hand, Weapon w = new Gun(...) does. Commented Feb 21, 2014 at 0:48
  • Personally, no, it's not, technically Collection, unless all the objects you are trying to add are based from a common parent, then it might be, but considering you are trying to provide functionality different objects types, I'd personally avoid it. Instead, I would use a List internally for each type of object you want to included, assuming you can have more than one. Commented Feb 21, 2014 at 0:48
  • If the desired behavior of Weapon ist not only similar to, but actually the same as the behavior of Collections (as I understood), why do you want to reimplement it? Just reuse the classes the Java API offer. Extending is an option, but also just adding a collection as a class member is easy to implement. Commented Feb 21, 2014 at 0:49

4 Answers 4

1
List<Object> weapons = new ArrayList<Object>();    

weapons.add(new Sword());     
weapons.add(new Gun());

try this.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't necessarily have to do a new Gun() when you instantiate. You can just do the following:

class Weapon
{
    private Gun gun;
    public Weapon()
    {
        gun = new Gun(); //etc.
    }
    //add more methods here. You can use gun.function() to invoke any functions
}

As for using a Weapon object, just declare public methods in the Weapon class and then invoke them in your methods.

For example, the add() function:

class Weapon
{
    ...
    public int val() { return 0; } // don't know, some random method.
}

Then on a different file:

int add(Weapon w)
{
    return Weapon.val() + 1111; //perform something here
}

I hope this answers your questions, it was a little bit unclear :|. Comment if you need more help.

Comments

0

Your question is a bit unclear to me, but I'll try my best.

If you want to have a List of Weapon, then you need to use inheritance. In your example, A Gun is-a Weapon. Therefore, you can do class Gun extends Weapon. Then, let's say you're using an ArrayList, you would instantiate it like:

List<Weapon> weapons = new ArrayList<Weapon>();

You can now call add with a Gun, or any other sub-class of Weapon like so:

weapons.add(new Gun()); //or
weapons.add(new Cannon());

If you actually need to add a Gun to a Weapon (Which doesn't really make sense...), you can use composition. See Shahar's answer for a more detailed explanation of this behavior.

Just to clarify, no matter what, I'm pretty sure creating a new Collection is not what you want to do here.

Comments

0

These are a lot of questions :-) let's enumerate them

  1. creating a method called add() into an object makes this object a Collection? No. To be a Collection, a class must implement the interface java.util.Collection or implement any one of the subinterfaces or extend any of the subclasses that implement this interface. A list of them is available in the Collection javadoc - http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
  2. if I want to add an object into another, it must be via the constructor? e.g. new Weapon(new Gun())? Not necessarily. You can just create a Weapon w = new Weapon() and then add the Gun reference via a method such as w.setGun(new Gun()). Anyway, any constructor with one or more parameters must be declared explicitly in your class. Only the non-parameter constructor is available by default.

Now, let's suppose that you want to store a class Gun that is a subclass of a class Weapon. Then you first need to define the hierarchy between them, for example

class Weapon{       
}

class WeaponWithBlade extends Weapon{       
}

class Sword extends WeaponWithBlade{        
}

class Knife extends WeaponWithBlade{        
}

class Gun extends Weapon{       
}

and then you want to store all these weapons in a Weapon collection.

since all of them are subclasses of the Weapon class, it's valid to do something like this

    java.util.List<Weapon> weapons = new java.util.ArrayList<>();
    weapons.add(new Sword());
    weapons.add(new Gun());

notice that both List and ArrayList are collections (List is an interface and ArrayList is a concrete class that implements the List interface), parametrized to be collections of Weapons.

I hope this can help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.