0

I've read several times, for instance here https://stackoverflow.com/a/589885/1420898, that static members are a bad OO design and should be avoid. It also happens in the project I'm working on; they've used Interfaces + implementations for classes that seems to me should be static classes, since we just use its functions to perform some operations on other classes.

My question is, why should we use Interfaces over static classes?

Community
  • 1
  • 1
Luis Sep
  • 2,384
  • 5
  • 27
  • 33

2 Answers2

1

In terms of using a static class versus an interface, a static class would be used as a helper, i.e. it would be a single instance of the class, whereas a class being instantiated through an interface may have many different implementations, and is generally used for multiple instance classes. You use a static class when you just want a class to do something, and not store state information particular to that call. An interface would be used more for general Object Oriented programming.

Static Classes are basically used when we want an object to be used without instantiation or making its object. Usually it happens that we place our common functions or such functions that are repeatedly used in static classes. As putting them in simple class will have lots of overhead as each time class object will be formed it will be allocating memory to all we have in it. So it reduces out overhead by using static Classes.

When comparing to interfaces while there is no as such comparison it depends on your requirements, if requirements are like above then surely u would go for static class rather than interfaces but if we have such requirement that we have several class and we want they should be following such a pattern or implementing these things before their object is formed then we would be preferring for interfaces rather than static classes.

Moreover if my functions are such that they are common but have different implementation corresponding to different classes then i would be again going for interfaces rather than static classes.

Imran Jawaid
  • 471
  • 1
  • 10
  • 27
0

first of all, I would say, there is NO static class in java. well, except for static inner class. I thought inner class is definitely not what you meant above there.

You might wanna say, a class that not allowed to be instantiated, with static methods. Like Util classes. i.e. apache common StringUtil...

Static methods in Util classes provide only one implementation. and more important is, it should not know much detailed logic about the passed in Object.

making an example, you have interface:

    interface Animal{
   int totalLegs(); //here you get the animal object has how many legs 
}

then you have Bird, Horse, Warm, Fly classes, implement Animal interface. and they have different impl. of totalLegs. Those implementations are related to detailed logic of the type Animal (Bird, Horse...) so it should go to interface.

And say each Animal has a field Date birthday; you want to convert the Date to a String with customized pattern. You could create a class, i.e DateUtil and method public static String getDateString(Date d, String pattern){...}

Maybe you could describe a little bit about the interface/impl. in your project. Then we could see if it is better to go to a Util class.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • 'static inner class' is a contradiction in terms. 'static nested class' is OK. An 'inner class' is non-static by definition. See the JLS. – user207421 Oct 02 '12 at 10:23