0

We use abstract classes if we need to keep an implemented method and method definitions while we use interfaces if we need to keep just the method definitions. But in java and C# we can use default methods and static methods to include implementations in interfaces. So what is the use of abstract classes if we can achieve what it is intended to do by using interfaces?

2

1 Answer 1

1

There are some reasons to have abstract classes.

Abstract classes can inherit interfaces, but not vice versa

Abstract classes can inherit interfaces. However, interface cannot inherit abstract class:

public interface ABar { }

public abstract class Bar : IFoo { }

However, this is not eligible:

public interface IFoo : ABar
{ 

}

public abstract class ABar
{ 
}

Abstract classes can have constructors, fields (can hold state)

Abstract classes can have constructors, fields. By having fields, it is possible to hold state. However, interface cannot have them.

public abstract class ABar
{
    public int test; // some state

    public ABar()
    {

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.