A class should be considered as an object or module that performs a key role or function in a program. A role that no class or module performs. For example, you can have an Animal class that provides sleep() , run() functions. But you may need a class for carnivores that also kill() , hunt() , etc. So, you implement the Carnivores class, expanding from Animal , which makes all variables of type Animal , but also kills hunting in addition.
If your class has only one public method, but if it is important for design to have it as a separate module, then the class is good for it. You can renew it later if necessary.
You can also keep the sleep() and run() functions static and public, all Animal do this, and so you can just do Animal.sleep() , etc., without creating a separate instance. But functions like roar() should not be.
Update: The reason I said sleep() and run() may be static, there may be a class Man , which also sleeps and runs.
Question:
Does it make sense to call sleep() and run() or any function of a class without initializing an object of this class? If so, it makes sense to make it static.
sanjeev mk
source share