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
53
votes
3 answers

Why can't you call abstract functions from abstract classes in PHP?

I've set up an abstract parent class, and a concrete class which extends it. Why can the parent class not call the abstract function? //foo.php
Cam
  • 14,930
  • 16
  • 77
  • 128
51
votes
7 answers

Abstract Method in Ruby

How can I force a subclass to implement a method in Ruby. There doesn't seem to be an abstract keyword in Ruby, which is the approach I would take in Java. Is there another more Ruby-like way to enforce abstract?
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
51
votes
8 answers

Why does Java not allow multiple inheritance but does allow conforming to multiple interfaces with default implementations

I am not asking this -> Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed? In Java, multiple inheritance isn't allowed, but, after Java 8, Interfaces can have default methods (can implement methods…
Asanka
  • 1,628
  • 1
  • 10
  • 20
50
votes
5 answers

Best way to declare an interface in C++11

As we all know, some languages have the notion of interfaces. This is Java: public interface Testable { void test(); } How can I achieve this in C++ (or C++11) in most compact way and with little code noise? I'd appreciate a solution that…
emesx
  • 12,555
  • 10
  • 58
  • 91
44
votes
9 answers

Java final abstract class

I have a quite simple question: I want to have a Java Class, which provides one public static method, which does something. This is just for encapsulating purposes (to have everything important within one separate class)... This class should neither…
Sauer
  • 1,429
  • 4
  • 17
  • 32
44
votes
8 answers

C#, implement 'static abstract' like methods

I recently ran into a problem where it seems I need a 'static abstract' method. I know why it is impossible, but how can I work around this limitation? For example I have an abstract class which has a description string. Since this string is common…
Calmarius
  • 18,570
  • 18
  • 110
  • 157
44
votes
4 answers

Derive abstract class from non-abstract class

Is it OK to derive an abstract class from a non-abstract class or is there something wrong with this approach? Here´s a little example: public class Task { // Some Members } public abstract class PeriodicalTask : Task { // Represents a base…
Jehof
  • 34,674
  • 10
  • 123
  • 155
44
votes
15 answers

Instantiating interfaces in Java

I have this interface: public interface Animal { void Eat(String name); } And this code here implements the interface: public class Dog implements Animal { public void Eat(String food_name) { System.out.printf(food_name); } …
user1535147
  • 1,325
  • 5
  • 14
  • 21
40
votes
3 answers

The designer must create an instance of...cannot because the type is declared abstract

Visual Studio complains: Warning 1 The designer must create an instance of type 'RentalEase.CustomBindingNavForm' but it cannot because the type is declared as abstract. Visual Studio won't let me access the Designer for the form. The class…
Malfist
  • 31,179
  • 61
  • 182
  • 269
38
votes
1 answer

Angular4 Components inheritance with abstract class

I want to define a base class for my components to share some features. So i've began with : export abstract class BaseComponent { protected getName(): string; } @Component(...) export class MyComponent extends BaseComponent { protected…
Chklang
  • 857
  • 1
  • 8
  • 17
37
votes
4 answers

Can we instantiate an abstract class directly?

I have read we can only instantiate an abstract class by inheriting it, but we cannot instantiate it directly. However, I saw we can create an object with the type of an abstract class by calling a method of another class. For example -…
satheesh.droid
  • 29,947
  • 10
  • 34
  • 34
36
votes
14 answers

Django: Best way to unit-test an abstract model

I need to write some unit tests for an abstract base model, that provides some basic functionality that should be used by other apps. It would be necessary to define a model that inherits from it just for testing purposes. Are there any…
Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
36
votes
3 answers

ForeignKey field related to abstract model in Django

I have this model: class BaseModel(models.Model): .... class Meta: abstract = True class ModelA(BaseModel): .... class ModelB(BaseModel): .... class MyExtModel(models.Model) myfield =…
Safari
  • 11,437
  • 24
  • 91
  • 191
36
votes
3 answers

PHP Interface: Specify ANY visibility

I'm making use of an interface for a set of classes. I have a problem however because I wish for any visibility to be allowed in the interface (That is: public, protected and private). I need the parent method to only be protected and I need the…
TheFrack
  • 2,823
  • 7
  • 28
  • 47
34
votes
6 answers

Overriding an abstract property with a derived return type in c#

I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property with the following declaration: public abstract Request request { get; set; } The DerivedHandler needs to override this property so that it…
Trevor
  • 13,085
  • 13
  • 76
  • 99