Contents
- 1 Can an abstract class be empty?
- 2 What is the benefit of making an empty abstract method in a base class?
- 3 Do abstract classes have to be implemented?
- 4 Can a virtual method be empty?
- 5 How to force an abstract class to call a constructor?
- 6 What happens if the base class has no constructor?
- 7 Can a derived class call a concrete constructor?
Can an abstract class be empty?
The key is that you can extend from only one abstract class, while you can implement more interfaces. Apparently the “empty abstract class” design desicion was made so that it prevents the implementing class from extending from another classes.
What is the benefit of making an empty abstract method in a base class?
It allows you to add abstract methods to your superclass (and implementations to the subclasses) later, without affecting any existing clients. The abstract keyword works even if the non-leaf class does not currently have any abstract methods.
What can an abstract class not do?
Abstract methods cannot have body. Abstract class can have static fields and static method, like other classes. An abstract class cannot be declared as final.
Do abstract classes have to be implemented?
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
Can a virtual method be empty?
It’s a perfectly valid structure. It allows your base code to call it. You tend to see similar designs when there is “BeforeXXX” and “AfterXXX” code, at the base class this code is empty, but the method needs to be there to compile. In derived classes, this code is optional, but needs to be virtual to be overridden.
Can an abstract class extend concrete?
An abstract class always extends a concrete class ( java. lang. Object at the very least). So it works the same as it always does.
How to force an abstract class to call a constructor?
Make parameterless constructor in your abstract class private, or not add it at all. That’s will force all derived classes to call the constructor you specified or there will be a compile error. You don’t need to do anything.
What happens if the base class has no constructor?
If the base class has no parameterless constructor (as is the case if you add a constructor accepting a string and don’t explicitly add a parameterless constructor), the class will not compile.
Is there benefit to empty classes in C + +?
In C++ you have Multiple Inheritance so there is literally no benefit to having those empty classes. If you want to have Ball inheriting from GameObject and MovableObject later (say, for example, you want to hold an array of GameObjects and call a Tick method every nth of a second to make them all move), that’s easy enough to do.
Can a derived class call a concrete constructor?
If you want to be 100% sure derived classes will call a concrete base constructor, you can implement your base class using a single parameterless constructor with optional parameters and use this instead of constructor overloading: Now if a B class derived A, B will always call A ‘s base constructor, since x is optional and it has a default value.