Nerd For Tech
Published in

Nerd For Tech

Abstract Classes vs. Interfaces

Abstract Classes

An abstract class is a class that has been declared abstract and has the option to include abstract methods. They cannot be instantiated, but they can be used as subclasses.

Interfaces

Interfaces are similar to classes except that an interface represents the contract between the object and the user. They cannot be instantiated and they cannot be used to define data. An interface is a class that is used to define methods and properties.

Differences Between Abstract Classes and Interfaces

One difference is that abstract classes are allowed to define data.

An interface is not allowed to define data.

Another difference is that classes can only inherit one abstract class but can inherit multiple interfaces.

One of the biggest differences is that if a class uses an interface it has to implement all the methods and properties declared in the interface. If a class uses an abstract class it can decide which methods and properties to use as long as they are not declared as abstract.

Note: Skeleton has to use both Health and Damage() but does not have to use speed or gems

Summary

An abstract class is used when you are defining what an object is. For example, saying an apple is a fruit. An interface is used when you are defining what a class can do. For example, a chief is capable of cooking.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store