Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need for Abstract Class as well as Interface?

An interface is a 100% abstract class, so we can use an interface for efficient programming. Is there any situation where an abstract class is better than an interface?

like image 785
Warrior Avatar asked Nov 27 '08 04:11

Warrior


People also ask

Why do we need both interface and abstract classes?

Multiple implementations: An interface can extend one or more Java interfaces; an abstract class can extend another Java class and implement multiple Java interfaces. Multiple Inheritance: Interface supports multiple inheritance; an abstraction does not support multiple inheritance.

Can we use abstract class and interface together?

The instance of an abstract class can't be created. Now as all methods in an interface are abstract methods therefore we can implement it using Abstract Class.

What is the need of abstract class?

An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class.

Why are abstract classes needed over interface explained with real life example?

Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. On the other hand, if you use interfaces, you would need to implement all the methods in the class that extends the interface.


1 Answers

Abstract classes are used when you do intend to create a concrete class, but want to make sure that there is some common state in all the subclasses or a possible common implementation for some operations.

Interfaces cannot contain either.

like image 67
Uri Avatar answered Oct 21 '22 07:10

Uri