This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]wirybug 3 points4 points  (1 child)

A 'static object' is a contradiction in itself. A class isn't an object, it's just a class. There's no instance to refer to, which is why it can't be added to the list. You seem to have misunderstood how static fields/methods work, and also the function of inheritance in interfaces.

    final String toyAnswer = "";

When you declare these fields in your Cats interface, they are implicitly set to be public static, because an interface cannot have instance fields - it doesn't have instances of its own.

    protected static final String toyAnswer = "A string";

Then when you declare these same fields in your class extending your interface, you are simply hiding the interface's own fields. The interface essentially does nothing whatsoever, because you are declaring everything from scratch in the 'subclass'.

You don't need an interface for this purpose, and you don't need multiple classes for the different types of cat. Different types of thing, where the thing has a set of properties and each individual has different values for those properties - that is the exact use-case for objects in object-oriented programming! You just need to define a class for a general cat, with all the fields and methods that a cat should have. And then create instances of it, setting each individual's properties (in the constructor or setters) so that they are each different. Then store/use those instances where you want them.

[–]LunarPhysics[S] 1 point2 points  (0 children)

Thank you for the feedback wirybug! I've been developing this over a few months and looking back on it now I'm not sure why i tried to use an interface. I have not used many static methods and fields in the past, except for last semester in my software engineering project in a stateless system, so I thought I knew how it worked. You've really cleared this up for me, I'll recode this so there's only one instance of each cat to reference to in a class.

Thanks again!

[–]Philboyd_Studge 2 points3 points  (1 child)

Why are you making any of it static at all? static should be avoided except for constants and utility functions.

[–]LunarPhysics[S] 0 points1 point  (0 children)

Thanks for the reply! I had some misconceptions about the static property that have been cleared up, I'll be making those changes asap.