Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of abstract classes in Java? [duplicate]

I have some questions around abstract classes and their use. I know the basics about them; for example, that they can't be instantiated, they can have concrete and abstract methods, ... But I guess what I wanna know is, what purpose do they serve in life (in software engineering)?

What is the purpose of abstract classes in Java? Why and when should one use an Abstract class? If you can use a normal class and then inherit it, why would you inherit an abstract class? How would using abstract classes make our life easier? would it provide better maintainability? more flexibility? ...

btw, I've already looked at some similar questions and answers, including Understanding the purpose of Abstract Classes in Java, but they don't answer my question. I'm more looking for answers that shed light on the philosophy behind the introduction of abstract classes in the first place in Java.

Thanks

like image 646
Spiky Avatar asked Apr 07 '15 17:04

Spiky


People also ask

What is the purpose of abstract classes in Java?

Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation. We can run abstract class in java like any other class if it has main() method.

What is the purpose of using abstract classes?

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

What is purpose of abstract class Mcq?

Explanation: Abstract class is used to design base class because functions of abstract class can be overridden in derived class hence derived class from same base class can have common method with different implementation, hence forcing encapsulation.

Can an abstract class be inherited multiple times?

A class can inherit from multiple abstract classes.


1 Answers

An abstract class can be used as a type of template for other classes, and most commonly used for inheritance.

The best example is from the book head first java:

abstract class Animal(){}

then you can extend child class such as: dog, cat, fish.

like image 168
OPK Avatar answered Nov 15 '22 18:11

OPK