Questions tagged [abstract]

abstract is a keyword shared by a multitude of object-oriented programming languages. Methods and classes can be marked abstract to indicate that they do not contain the full implementation of application logic and have to be extended. Abstract classes can not be instantiated and serve the purpose of providing a uniform interface for their subclasses, as well as the implementation of common methods that don't have to be reimplemented for each subclass.

abstract is a keyword shared by a multitude of object-oriented programming languages.

Methods and classes can be marked abstract to indicate that they do not contain the full implementation of application logic. Abstract classes can not be instantiated and serve the purpose of providing a uniform interface for their subclasses, as well as the implementation of common methods that don't have to be reimplemented for each subclass.

The exact meaning of abstract depends on the programming languages in question, some of them being: Java, C#, php, C++, Delphi Pascal. Similar logic can also be implemented using different keywords in other languages ( for example, Oracle PL/SQL allows to create abstract classes and methods by declaring them NOT FINAL)

The power of abstract methods and classes is widely used by design patterns.

2521 questions
1714
votes
27 answers

What is the difference between an abstract method and a virtual method?

What is the difference between an abstract method and a virtual method? In which cases is it recommended to use abstract or virtual methods? Which one is the best approach?
Moran Helman
  • 18,432
  • 4
  • 23
  • 26
629
votes
16 answers

Can we instantiate an abstract class?

During one of my interview, I was asked "If we can instantiate an abstract class?" My reply was "No. we can't". But, interviewer told me "Wrong, we can." I argued a bit on this. Then he told me to try this myself at home. abstract class my { …
Ravi
  • 30,829
  • 42
  • 119
  • 173
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
349
votes
14 answers

Extend data class in Kotlin

Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this: open data…
Dmitry
  • 4,232
  • 2
  • 15
  • 13
293
votes
6 answers

Abstract methods in Python

I am having trouble in using inheritance with Python. While the concept seems too easy for me in Java yet up till now I have been unable to understand in Python which is surprising to me at least. I have a prototype which follow: class Shape(): …
user506710
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
218
votes
9 answers

Java abstract interface

Consider an example (which compiles in java) public abstract interface Interface { public void interfacing(); public abstract boolean interfacing(boolean really); } Why is it necessary for an interface to be "declared" abstract? Is there…
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
199
votes
3 answers

How can I determine whether a Java class is abstract by reflection

I am interating through classes in a Jar file and wish to find those which are not abstract. I can solve this by instantiating the classes and trapping InstantiationException but that has a performance hit as some classes have heavy startup. I can't…
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
165
votes
18 answers

How do I create an abstract base class in JavaScript?

Is it possible to simulate abstract base class in JavaScript? What is the most elegant way to do it? Say, I want to do something like: var cat = new Animal('cat'); var dog = new Animal('dog'); cat.say(); dog.say(); It should output: meow bark
Sabya
  • 11,534
  • 17
  • 67
  • 94
146
votes
4 answers

Why would one declare a Java interface method as abstract?

I used the "pull interface" refactoring feature of Eclipse today to create an interface based on an existing class. The dialog box offered to create all the new methods of the new interface as "abstract" methods. What would be the benefit of that? I…
Uri
  • 88,451
  • 51
  • 221
  • 321
129
votes
12 answers

Defining an abstract class without any abstract methods

Can I define an abstract class without adding an abstract method?
VisaMasterCard
  • 1,666
  • 4
  • 18
  • 22
125
votes
5 answers

Why not abstract fields?

Why can't Java classes have abstract fields like they can with abstract methods? For example: I have two classes that extend the same abstract base class. These two classes each have a method that is identical except for a String constant, which…
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
122
votes
8 answers

Why does PHP 5.2+ disallow abstract static class methods?

After enabling strict warnings in PHP 5.2, I saw a load of strict standards warnings from a project that was originally written without strict warnings: Strict Standards: Static function Program::getSelectSQL() should not be abstract in…
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
120
votes
8 answers

Abstract attribute (not property)?

What's the best practice to define an abstract instance attribute, but not as a property? I would like to write something like: class AbstractFoo(metaclass=ABCMeta): @property @abstractmethod def bar(self): pass class…
Lapinot
  • 1,305
  • 2
  • 9
  • 11
119
votes
6 answers

Why are C# interface methods not declared abstract or virtual?

C# methods in interfaces are declared without using the virtual keyword, and overridden in the derived class without using the override keyword. Is there a reason for this? I assume that it is just a language convenience, and obviously the CLR…
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
1
2 3
99 100