Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class derived from concrete class

Tags:

java

oop

I've just got a warning in my IDE that my class is abstract but it is derived from concrete class. And what? Why can't I do so? This suites my needs very well. It's just a warning though so the class is compilable. Just interested why it gives me this warning. Thank you.

Update

I need to extend my question with some explanations. I have this class derived from other class and I want nobody to be able to instantiate it. Moreover, I need two subclasses from this abstract class and each of them to have their own implementation of one abstract method. That's why.

Do you think it is bad idea? What approach shall I take instead?

like image 660
Eugene Avatar asked Feb 15 '13 11:02

Eugene


People also ask

Can abstract class inherit from concrete class C#?

In C#, two classes (either abstract or concrete) cannot be inherited by the same derived class.

Can abstract class be derived from non-abstract class?

An abstract method must be implemented in all non-abstract classes using the override keyword. After overriding, the abstract method is in the non-Abstract class. We can derive this class in another class, and again we can override the same abstract method with it.

Can a class be abstract with concrete 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. Abstract class can not be instantiated using new keyword.

Can abstract classes be derived?

Note that you can derive an abstract class from a nonabstract class, and you can override a non-pure virtual function with a pure virtual function. You can call member functions from a constructor or destructor of an abstract class.


2 Answers

I think the reason is maybe that abstract classes usually serve for basic behavior/information for creating subclasses. Think about other programmers while making such a decision. Others don't expect it.

Anyway, you can set compiler preferences not to give warnings for this type of "problem".

Update

After reading your update I think the simplest way is to declare this class's constructor protected. For the abstract method: you can simply override that from your subclasses, I think.

like image 196
qben Avatar answered Sep 23 '22 00:09

qben


You can do that. That's why you got a warning and not an error.

This is a common case and typically points to bad system architecture. That's why the compiler shows a warning.

like image 36
Damian Leszczyński - Vash Avatar answered Sep 27 '22 00:09

Damian Leszczyński - Vash