Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some practical examples of abstract classes in java?

When and why should abstract classes be used? I would like to see some practical examples of their uses. Also, what is the difference between abstract classes and interfaces?

like image 666
Ali Avatar asked Oct 02 '09 14:10

Ali


1 Answers

Abstract classes are "half-implementations" of a class. They can be partially implemented with some generic functionality, but leave part of the implementation to the inheriting classes. You could have an abstract class called Animal that has implemented some generic behavior/values such as Age, Name, SetAge(...). You can also have methods that are not implemented (they are abstract), much like an interface.

Interfaces are simply contracts that specify behaviors that should be available for a class. You could have an interface such as IWalker that requires public method Walk(), but no specifics on how it is implemented.

like image 112
Blixt Avatar answered Nov 15 '22 19:11

Blixt