Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of an abstract class without any abstract methods

An abstract class need not include any abstract methods.

Is there any other reason to make a class abstract other than the fact that abstract classes can't be instantiated?

like image 595
gifpif Avatar asked Aug 03 '13 06:08

gifpif


People also ask

What is the use of abstract class without abstract method?

Abstract class without abstract method means you can create object of that abstract class. See my Example. If you write one abstract method inside abstract class then it will not compile. Which means if you create abstract class without abstract method then you can create Object of that Abstract Class.

Can we have an abstract class without any abstract methods?

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.

What is the use of abstract class without abstract method in C#?

Abstract Class: This is the way to achieve the abstraction in C#. An Abstract class is never intended to be instantiated directly. An abstract class can also be created without any abstract methods, We can mark a class abstract even if doesn't have any abstract method.


2 Answers

Abstract class means the definition of the class is not complete and hence cannot be instantiated. Even though it does not have abstract method, it is an indicator that the class is available for inheritance. Even though it has implementation for all the methods in it, the implementation may still not be complete and must be overridden by the extending class.

like image 176
Ankur Shanbhag Avatar answered Oct 06 '22 00:10

Ankur Shanbhag


The principal role of an abstract class is to provide an appropriate root class from which concrete, (i.e. non-abstract) subclasses can be derived. This is a powerful and versatile feature which promotes code re-use. Abstract classes encapsulate general features that are common to a range of data types - features which are too general to be meaningful in the abstract class, but which can be overridden in a subclass

Any class with an abstract method is automatically abstract itself and must define itself as such with the keyword abstract - interestingly, an abstract class need not contain any abstract methods

An abstract class cannot be instantiated - in other words you cannot create instances (objects) of an abstract class

References to objects of an abstract class can be declared even though objects of abstract classes cannot be instantiated, e.g Account a ; will not generate a syntax error

If a subclass of an abstract class overrides, i.e. provides an implementation of every abstract method in its superclass, the subclass is called a concrete class and objects of the subclass can be created

If a subclass of an abstract class does not override (implement) all of the abstract methods it inherits, that subclass itself is also abstract and must be declared as such

like image 29
Ushani Avatar answered Oct 06 '22 02:10

Ushani