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").
4696 questions
1835
votes
37 answers

What is the difference between an interface and abstract class?

What exactly is the difference between an interface and abstract class?
Sarfraz
  • 355,543
  • 70
  • 511
  • 562
1475
votes
35 answers

Interface vs Abstract Class (general OO)

I have had recently 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
  • 58,044
  • 75
  • 235
  • 407
828
votes
16 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
  • 19,913
  • 17
  • 69
  • 108
666
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
  • 13,926
  • 10
  • 35
  • 40
620
votes
26 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
  • 36,343
  • 33
  • 109
  • 143
588
votes
15 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
  • 38,384
  • 18
  • 73
  • 117
579
votes
8 answers

Difference between abstract class and interface in Python

What is the difference between abstract class and interface in Python?
gizmo
  • 7,229
  • 6
  • 21
  • 21
507
votes
21 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,530
  • 4
  • 31
  • 47
498
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,342
  • 9
  • 23
  • 46
468
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
  • 15,841
  • 12
  • 46
  • 79
457
votes
23 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
  • 14,264
  • 7
  • 45
  • 71
379
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
342
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
284
votes
14 answers

Abstract class in Java

What is an "abstract class" in Java?
keyur
246
votes
5 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
  • 21,347
  • 16
  • 59
  • 94
1
2 3
99 100