Questions tagged [abstract-class]

Abstract classes are classes which cannot be instantiated. They exist to provide common functionality and interface specifications to several concrete classes.

Abstract classes are classes which cannot be instantiated. They exist to provide common functionality and interface specifications to several concrete classes.

From Abstract Type on Wikipedia:

Abstract classes can be created, signified, or simulated in several ways:

  • By use of the explicit keyword abstract in the class definition, as in Java, D or C#.
  • By including, in the class definition, one or more methods (called pure virtual functions in C++), which the class is declared to accept as part of its protocol, but for which no implementation is provided.
  • By inheriting from an abstract type, and not overriding all missing features necessary to complete the class definition.
  • In many dynamically typed languages such as Smalltalk, any class which sends a particular method to this, but doesn't implement that method, can be considered abstract. (However, in many such languages, the error is not detected until the class is used, and the message returns results in an exception error message such as "does Not Understand").
5262 questions
263
votes
15 answers

Constructor of an abstract class in C#

Why is it possible to write constructor for an abstract class in C#? As far as I know we can't instantiate an abstract class.. so what is it for? You can't instantiate the class, right?
Mulder
  • 2,817
  • 3
  • 17
  • 7
254
votes
12 answers

Using Mockito to test abstract classes

I'd like to test an abstract class. Sure, I can manually write a mock that inherits from the class. Can I do this using a mocking framework (I'm using Mockito) instead of hand-crafting my mock? How?
ripper234
  • 222,824
  • 274
  • 634
  • 905
249
votes
5 answers

Declaring abstract method in TypeScript

I am trying to figure out how to correctly define abstract methods in TypeScript: Using the original inheritance example: class Animal { constructor(public name) { } makeSound(input : string) : string; move(meters) { …
Vojtěch
  • 11,312
  • 31
  • 103
  • 173
245
votes
7 answers

How to create abstract properties in python abstract classes?

In the following code, I create a base abstract class Base. I want all the classes that inherit from Base to provide the name property, so I made this property an @abstractmethod. Then I created a subclass of Base, called Base_1, which is meant to…
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
196
votes
1 answer

Does ECMAScript 6 have a convention for abstract classes?

I was surprised that I couldn't find anything about abstract classes when reading up on ES6. (By "abstract class" I'm talking about the Java meaning of it, in which an abstract class declares method signatures that a subclass must implement in order…
obelia
  • 2,105
  • 2
  • 13
  • 11
180
votes
5 answers

`staticmethod` and `abc.abstractmethod`: Will it blend?

In my Python app I want to make a method that is both a staticmethod and an abc.abstractmethod. How do I do this? I tried applying both decorators, but it doesn't work. If I do this: import abc class C(object): __metaclass__ = abc.ABCMeta …
167
votes
11 answers

Why is a pure virtual function initialized by 0?

We always declare a pure virtual function as: virtual void fun () = 0 ; I.e., it is always assigned to 0. What I understand is that this is to initialize the vtable entry for this function to NULL and any other value here results in a compile time…
mukeshkumar
  • 2,698
  • 3
  • 19
  • 20
165
votes
19 answers

Visual Studio: How do I show all classes inherited from a base class?

In Visual Studio, How do I show all classes inherited from a base class? For example, in ASP.NET MVC there are several 'ActionResult' types -- and they all inherit from / implement the base class ActionResult. It looks like unless you just…
Dan Esparza
  • 28,047
  • 29
  • 99
  • 127
161
votes
11 answers

Abstract classes in Swift Language

Is there a way to create an abstract class in the Swift Language, or is this a limitation just like Objective-C? I'd like to create a abstract class comparable to what Java defines as an abstract class.
kev
  • 7,712
  • 11
  • 30
  • 41
160
votes
3 answers

C#: Abstract classes need to implement interfaces?

My test code in C#: namespace DSnA { public abstract class Test : IComparable { } } Results in the following compiler error: error CS0535: 'DSnA.Test' does not implement interface member 'System.IComparable.CompareTo(object)' Since…
bguiz
  • 27,371
  • 47
  • 154
  • 243
160
votes
2 answers

How to create an instance of anonymous class of abstract class in Kotlin?

Assume that KeyAdapter is an abstract class with several methods that can be overridden. In java I can do: KeyListener keyListener = new KeyAdapter() { @Override public void keyPressed(KeyEvent keyEvent) { // ... } }; How to do the…
Tvaroh
  • 6,645
  • 4
  • 51
  • 55
159
votes
6 answers

Testing Abstract Classes

How do I test the concrete methods of an abstract class with PHPUnit? I'd expect that I'd have to create some sort of object as part of the test. Though, I've no idea the best practice for this or if PHPUnit allows for this.
Mez
  • 24,430
  • 14
  • 71
  • 93
153
votes
13 answers

Abstract attributes in Python

What is the shortest / most elegant way to implement the following Scala code with an abstract attribute in Python? abstract class Controller { val path: String } A subclass of Controller is enforced to define "path" by the Scala compiler. A…
deamon
  • 89,107
  • 111
  • 320
  • 448
148
votes
4 answers

Interfaces vs. abstract classes

In C#, when should you use interfaces and when should you use abstract classes? What can be the deciding factor?
tush1r
  • 19,443
  • 14
  • 36
  • 35
148
votes
6 answers

What is an abstract class in PHP?

What is an abstract class in PHP? How can it be used?
udaya
  • 9,598
  • 15
  • 48
  • 67