Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are abstract classes and abstract methods? [duplicate]

Tags:

java

oop

Possible Duplicate:
Abstract class in Java

I got several explanations but so far I'm not able to understand that what are the abstract classes and methods in Java.

Some said it has to do something with the security of the program, other said it isn't anything like that.

Even from the Dietel & Dietel's book I don't get it's purpose. When,where and why do we use it?

Kindly explain it like you're teaching a beginner, I would really appreciate your help.

like image 892
Umer Hassan Avatar asked Dec 11 '12 16:12

Umer Hassan


People also ask

What is difference between abstract class and abstract method?

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

Can abstract class have 2 abstract methods?

A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body).

Can an abstract class define both abstract methods and?

Yes, we can declare an abstract class with no abstract methods in Java. An abstract class means that hiding the implementation and showing the function definition to the user. An abstract class having both abstract methods and non-abstract methods. For an abstract class, we are not able to create an object directly.

Can abstract class have both concrete and abstract methods?

Abstract class can have both an abstract as well as concrete methods. A concrete class can only have concrete methods. Even a single abstract method makes the class abstract.


2 Answers

- Abstract class is one which can't be instantiated, i.e. its object cannot be created.

- Abstract method are method's declaration without its definition.

- A Non-abstract class can only have Non-abstract methods.

- An Abstract class can have both the Non-abstract as well as Abstract methods.

- If the Class has an Abstract method then the class must also be Abstract.

- An Abstract method must be implemented by the very first Non-Abstract sub-class.

- Abstract class in Design patterns are used to encapsulate the behaviors that keeps changing.

like image 80
Kumar Vivek Mitra Avatar answered Nov 04 '22 09:11

Kumar Vivek Mitra


An abstract class is a class that can't be instantiated. It's only purpose is for other classes to extend.

Abstract methods are methods in the abstract class (have to be declared abstract) which means the extending concrete class must override them as they have no body.

The main purpose of an abstract class is if you have common code to use in sub classes but the abstract class should not have instances of its own.

You can read more about it here: Abstract Methods and Classes

like image 30
Aviram Segal Avatar answered Nov 04 '22 09:11

Aviram Segal