I started out with a generic interface called ILogin. The interfaces requires that you implement two properties: UserID and Password. I have many login-type classes that implement this interface. As my project grew and grew, I found that many classes repeated the UserID and Password code. Now I decide that I need a base Login class.
Is it proper to create an abstract base Login class that implements the ILogin interface and have all of my concrete classes just inherit from the abstract class and override when necessary? Originally I was thinking there would be no problem with this. Then I started think that ILogin was probably unneeded because it'll likely only ever be implemented by my abstract class.
Is there a benefit to keeping both the abstract class and the interface around?
Thanks!
Now as all methods in an interface are abstract methods therefore we can implement it using Abstract Class.
If you are creating functionality that will be useful across a wide range of objects, then you must use an interface. Abstract classes, at the end of the day, should be used for objects that are closely related. But the interfaces are best suited for providing common functionality to unrelated cases.
In simple Language : Use interface if you want your objects be accessed by common way. Use abstract class if you want to define some functionality in super class and to define prototype of some methods that must be override in child classes i.e., extending the functionality of a class.
The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes. For this reason multiple inheritance is block using classes in java.
Definitely. Let's think of a concrete example.
Say we have an abstract class Animal
.
Say, we make some subclasses Cat
, Dog
, Mosquito
, and Eagle
. We can implement its Eat()
, Breathe()
, Sleep()
methods of the abstract class Animal
.
So far, so good. Now, let's say we want to have the Fly()
method for the Mosquito
and Eagle
classes. Since these two organisms aren't really well-related (one is a bird, another is an insect) it wouldn't be easy to come up with a common ancestor for the two that we can have as an abstract class. This would best be implemented by an interface IFly
.
The IFly
interface can have a Fly()
method to be implemented. Both Mosquito
and Eagle
classes can both be subclasses of the abstract class Animal
and implement the interface IFly
and be able to Eat()
, Breathe()
, Sleep()
and Fly()
without having some type of odd ancenstral relationship between the two classes.
I usually code against abstract classes when it makes sense and implement (and create in an external contracts assembly/library) an interface in every class (abstract or not) so I can more easily implement Windows Communication Foundation or inversion of control when necessary (which is almost always for mocking). This has become second nature for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With