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
34
votes
2 answers

Can one declare a static method within an abstract class, in Dart?

In an abstract class, I wish to define static methods, but I'm having problems. In this simple example abstract class Main { static String get name; bool use( Element el ); } class Sub extends Main { static String get name => 'testme'; bool…
cc young
  • 18,939
  • 31
  • 90
  • 148
33
votes
3 answers

Abstract methods in Java

I want to write an abstract method but the compiler persistently gives this error: abstract methods cannot have a body I have a method like this: public abstract boolean isChanged() { return smt else... } What is wrong here?
caner
  • 349
  • 1
  • 3
  • 4
33
votes
2 answers

Scala client composition with Traits vs implementing an abstract class

I have read that with Scala, it is generally advised to use Traits instead of Abstract classes to extend a base class. Is the following a good design pattern and layout? Is this how Traits were intended to replace Abstract? client class (with def…
kliew
  • 3,073
  • 1
  • 14
  • 25
32
votes
9 answers

Java: static abstract (again) - best practice how to work around

I theoretically understand the point why there is no abstract static in Java, as explained for instance in Why can't static methods be abstract in Java . But how do I solve such a problem then? My application uses files of a few types, which I want…
Philippp
  • 847
  • 1
  • 8
  • 17
32
votes
9 answers

Is there a way to make sure classes implementing an Interface implement static methods?

First of all, I read erickson's useful reply to "Why can’t I define a static method in a Java interface?". This question is not about the "why" but about the "how then?". Edit: my original example was ill-posed, but I'll leave it below. While I am…
Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
32
votes
3 answers

Java - Abstract class to contain variables?

Is it good practice to let abstract classes define instance variables? public abstract class ExternalScript extends Script { String source; public abstract void setSource(String file); public abstract String getSource(); } The sub…
Adam Asham
  • 1,519
  • 2
  • 20
  • 32
32
votes
1 answer

c# Abstract Class implementing an Interface

I've seen the following code layout reading forums and other blog posts and adapted in order to ask a few questions. public interface IService { int Add(T entity); void Update(T entity); } public abstract class ServiceBase :…
fes
  • 2,465
  • 11
  • 40
  • 56
31
votes
2 answers

How to make an Abstract Class inherit from another Abstract Class in Python?

Is it possible to have an Abstract Class inheriting from another Abstract Class in Python? If so, how should I do this?
Squilliam
  • 313
  • 1
  • 3
  • 5
31
votes
5 answers

Override abstract readonly property to read/write property

I would like to only force the implementation of a C# getter on a given property from a base abstract class. Derived classes might, if they want, also provide a setter for that property for public use of the statically bound type. Given the…
Coincoin
  • 27,880
  • 7
  • 55
  • 76
29
votes
3 answers

Constructor injection into a base class using autofac

I have an abstract base controller which has a constructor I hoped would be populated by autofac when the controllers were built. public abstract class BaseController : Controller { protected ILogger { get; private set; } protected…
gav
  • 554
  • 1
  • 6
  • 10
29
votes
2 answers

Is it possible to specify a static function in a Kotlin interface?

I want to do something like this: interface Serializable { fun serialize(): ToType companion object { abstract fun deserialize(serialized: ToType): FromType } } or even this would work for me: interface…
Ky -
  • 30,724
  • 51
  • 192
  • 308
29
votes
9 answers

Benefits of using an abstract classes vs. regular class

I have decided to start doing small coding projects on my own that focus on code quality instead of code quantity and have a question about the use of abstract classes. Now I know the differences between abstract classes and interfaces with the…
ryanzec
  • 27,284
  • 38
  • 112
  • 169
28
votes
4 answers

How to get the name of the calling class (in PHP)

define('anActionType', 1); $actionTypes = array(anActionType => 'anActionType'); class core { public $callbacks = array(); public $plugins = array(); public function __construct() { $this->plugins[] = new admin(); …
Mark Tomlin
  • 8,593
  • 11
  • 57
  • 72
28
votes
4 answers

Is it OK to call abstract method from constructor in Java?

Let's suppose I have an abstract Base class that implements Runnable interface. public abstract class Base implements Runnable { protected int param; public Base(final int param) { System.out.println("Base constructor"); this.param…
Danylo Fitel
  • 606
  • 1
  • 6
  • 8
27
votes
4 answers

what is a abstract method on a interface in java

Possible Duplicate: Why would one declare a Java interface method as abstract? I found the following code in one of our ejb interfaces. Does anyone know what the abstract does in the interface? If you do please also explain why it might be needed…
Marthin
  • 6,413
  • 15
  • 58
  • 95