Rex
Rex

Reputation: 157

Replace switch case: interface vs abstract class

I have a code that has a switch statement and want to improve the design.

There's the interface and the the abstract class way.

I want to know which way is better and why ?

I have the following class :

enum MovieChargeType {regular, new_release }

class Movie {
    public MovieChargeType type;
    public double get_price(){
          switch (type){
                  case regular: //return regular price
                  case new_release : // return new_release price }
    } 

}

So I decided to improve the design using 2 ways:

1 Way - Interfaces

Interface MovieType{
    public double get_price();
}

class RegularMovie implements MovieType{
    public double get_price(){
        // return regular price
    }
}

class NewMovie implements MovieType{
    public double get_price(){
        // return new price
    }
}

class Movie{
        public MovieType type;
        public double get_price(){
              type.get_price();
        } 
    
    }

2 Way - abstract classes:

abstract class Movie {
        public MovieChargeType type;
        public abstract double get_price();
    
    }

class RegularMovie extends Movie{
        public double get_price(){
            // return regular price
        }
    }
    
    class NewMovie extends Movie{
        public double get_price(){
            // return new price
        }
    }

I want to know which one is better in such case? I've noticed that people tend to go with interfaces but can someone explain why?

Upvotes: 4

Views: 2098

Answers (2)

René Link
René Link

Reputation: 51373

I want to know which one is better in such case? I've noticed that people tend to go with interfaces but can someone explain why?

The inheritance dependency is the strongest of all dependencies, because your sub classes inherit all the dependencies that your parent classes have. E.g. if a parent class depends upon some library your sub class can only be used if that library is on the classpath. Maybe you faced a indirectly referenced from class file error sometime ago in your IDE. This error occures because of dependencies of your parent classes that are not on the compile classpath.

That's why most of the developers tend to use interfaces and keep the interface signature as easy as possible. I mean that you should not use any library classes in an interface's signature. Only use POJOs so that your interfaces only depend on pure Java and not depend upon other libraries.

Abstract classes can be very useful when you want to implement the template method pattern. The template method defines an abstract algorithm that can be extended by sub classes. They just override or implement abstract methods from the parent class. Abstract classes can implement behavior. So if there is a common behavior to all of your sub classes an abstract class might be good choice. But keep in mind that abstractions should be stable. If you have abstractions that change often all of your sub classes are affected. This problem can drammatically increase with each hierarchy level and make your code hard to maintain. A good rule is that the higher a class is in your hierarchy the more stable it must be.

Upvotes: 2

Aneesh
Aneesh

Reputation: 148

Abstract classes allow you to define the default behaviour of certain methods which the subclasses can override. Interfaces only allow you to define the signature of the methods, so each implementation will be different. If you would like some classes to have a default behaviour for a price, then use the abstract class design over the interface.

Upvotes: 1

Related Questions