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
1993
votes
38 answers

What is the difference between an interface and abstract class?

What exactly is the difference between an interface and an abstract class?
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1550
votes
34 answers

Interface vs Abstract Class (general OO)

I have recently had two telephone interviews where I've been asked about the differences between an Interface and an Abstract class. I have explained every aspect of them I could think of, but it seems they are waiting for me to mention something…
Houman
  • 64,245
  • 87
  • 278
  • 460
883
votes
17 answers

How do you declare an interface in C++?

How do I setup a class that represents an interface? Is this just an abstract base class?
Aaron Fischer
  • 20,853
  • 18
  • 75
  • 116
730
votes
22 answers

Can an abstract class have a constructor?

Can an abstract class have a constructor? If so, how can it be used and for what purposes?
Szere Dyeri
  • 14,916
  • 11
  • 39
  • 42
661
votes
30 answers

Why can't static methods be abstract in Java?

The question is in Java why can't I define an abstract static method? for example abstract class foo { abstract void bar( ); // <-- this is ok abstract static void bar2(); //<-- this isn't why? }
hhafez
  • 38,949
  • 39
  • 113
  • 143
636
votes
8 answers

Difference between abstract class and interface in Python

What is the difference between abstract class and interface in Python?
gizmo
  • 7,989
  • 6
  • 23
  • 22
629
votes
16 answers

When to use: Java 8+ interface default method, vs. abstract method

Java 8 allows for default implementation of methods in interfaces called Default Methods. I am confused between when would I use that sort of interface default method, instead of an abstract class (with abstract method(s)). So when should interface…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
538
votes
31 answers

How should I have explained the difference between an Interface and an Abstract class?

In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class. Here's my response: Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can…
Thinker
  • 6,820
  • 9
  • 27
  • 54
528
votes
25 answers

When to use an interface instead of an abstract class and vice versa?

This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage. When would one want to use an interface and when would one want to use an abstract class?
Chirantan
  • 15,304
  • 8
  • 49
  • 75
516
votes
14 answers

How to unit test abstract classes: extend with stubs?

I was wondering how to unit test abstract classes, and classes that extend abstract classes. Should I test the abstract class by extending it, stubbing out the abstract methods, and then test all the concrete methods? Then only test the methods I…
Paul Whelan
  • 16,574
  • 12
  • 50
  • 83
509
votes
22 answers

Creating an abstract class in Objective-C

I'm originally a Java programmer who now works with Objective-C. I'd like to create an abstract class, but that doesn't appear to be possible in Objective-C. Is this possible? If not, how close to an abstract class can I get in Objective-C?
Jonathan Arbogast
  • 9,620
  • 4
  • 35
  • 47
454
votes
13 answers

Is it possible to make abstract classes in Python?

How can I make a class or method abstract in Python? I tried redefining __new__() like so: class F: def __new__(cls): raise Exception("Unable to create an instance of abstract class %s" %cls) But now, if I create a class G that inherits…
user1632861
370
votes
11 answers

Interface or an Abstract Class: which one to use?

Please explain when I should use a PHP interface and when I should use an abstract class? How I can change my abstract class in to an interface?
user220758
306
votes
6 answers

Why use Abstract Base Classes in Python?

Because I am used to the old ways of duck typing in Python, I fail to understand the need for ABC (abstract base classes). The help is good on how to use them. I tried to read the rationale in the PEP, but it went over my head. If I was looking for…
Muhammad Alkarouri
  • 23,884
  • 19
  • 66
  • 101
297
votes
15 answers

Abstract class in Java

What is an "abstract class" in Java?
keyur
1
2 3
99 100