-2

I have the following code:

public class Toy {
    static protected int d = 1;;
    protected int id;
    protected double price;
    protected int minAge;
    protected int maxAge;
    protected String size;
    protected String name;

    public Toy(double price, int minAge, int maxAge, String size) {
        this.id = d++;
        this.price = price;
        this.maxAge = maxAge;
        this.minAge = minAge;
        this.size = size;
        String s = this.getClass().getName();
        this.name = size + "_" + s.substring(s.lastIndexOf('.') + 1) + "_" + id;
    }

    public double getPrice() {
        return price;
    }

    public int getId() {
        return id;
    }

    public String getSize() {
        return size;
    }

    public int getMinAge() {
        return minAge;
    }

    public int getMaxAge() {
        return maxAge;
    }

    public String getName() {
        return name;
    }

    public boolean check(double price, int minAge, int maxAge, String size) {
        if (this.price <= price && this.minAge <= minAge
                && this.maxAge >= maxAge && size.equals(this.size))
            return true;
        return false;
    }

    public String ToString() {
        return name + "_" + minAge + "-" + maxAge;
    }
} 

and some subclasses, one of them:

public class Ball extends Toy {

    public Ball(double price, int minAge, int maxAge, String size) {
        super(price, minAge, maxAge, size);
    }
}

I think, that this is no good way to create class, which does not have new fields/methods/.

Can you give me an advice if i am right? "What are the criteria for creating a new class? Should there be a new class for any different instance? Is it good to create a sub-class if it would not have any new fields/methods?"

  • 2
    What are you talking about? A subclass can have new fields and methods. Just declare them in the subclass. – nanofarad May 11 '14 at 13:40
  • I think what the guy wants to ask is something like this: "What are the criteria for creating a new class? Should there be a new class for any different instance? Is it good to create a sub-class if it would not have any new fields/methods?" However the question is very generic, I still think it is interesting and worth of asking. It's a common problem of Stack Overlow policy that such questions are rejected as too general. I personally like them :) @user3625577, is it what you are asking? If so, make it more clear in your question plase. – Honza Zidek May 11 '14 at 15:24
  • isn't this better for Code Review? – martijnn2008 May 11 '14 at 15:26

4 Answers4

1

The reason why we create classes is not primarily to "add more methods". Classes are building blocks of your object model. There serve as a type of your objects. They help to distinguish you (and the clients of your class and the future maintainters of your code) what is a different type a what is just an instance.

In your case of Toy vs. Ball, think about this: is a ball only an instance of The Toy, or is there anything like The Ball in your mind? Will you have methods of some other classes, which you want to accept only balls?

It doesn't matter if you now don't see any extra methods or properties of the ball. They may come later, or they don't have to come later. It doesn't matter. What matter is if in your model the ball is something special.

Do not overuse the inheritance, but do not consider it as a tool for adding methods or members to classes. This would be a complete basic misunderstanding of the concept.

Just be careful to follow the Liskov Substitution Principle. In a very short summary, once you decide to use inhteritance, try to design your class hierarchy so to avoid instantiating the "middle classes" (which still have descendants - which are not at the tail of the hiearchy). This would cause you problems later, see also the famous Square-Rectangle Problem".

There is a close relative concept called marker interface. You may notice that even in the standard Java API there are many interfaces without any methods. They add extra information to the objects, which may be used later.

Community
  • 1
  • 1
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
0

Usually one extends an existing class when the new one adds either new state or behavior.

Toy feels very generic to me. Could it be abstract, with Ball providing its special implementation of an abstract method?

If not, you sound like a Toy instance named ball would be enough.

If this state and methods are all you need, I'd make that class Toy a more generic thing like InventoryItem. Nothing toy-like that I can see.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

I don't see a problem in having a subclass without new fields/methods since the subclass adds one very important information: It is not just any toy, it is a ball. Perhaps another part of your code will depend on the fact that a specific instance is a ball instead of a more general toy.

And additional behavior or fields could come along later ...

ruediste
  • 2,434
  • 1
  • 21
  • 30
-1

if you don't need to add more methods of fields to your "Ball" , I think that this is the best way to do it:

Toy ball = new Toy(price,minaAge,maxAge,size);

you are using a powerful weapon as inheritance for a nobrainer , it's like using an army to kill a mosquito

  • The reason why we create classes is not primarily to "add more methods". The answer to his question is much more complicated than this. – Honza Zidek May 11 '14 at 14:59
  • I guess you are right , it depends on if you want to use the ball class as a mother for another type of classe (soccer ball , football ball .. ), so I think that making Toy abstract and Ball extending it will be practical – java_noob May 11 '14 at 15:47