Abstract attribute (not property)?

A possibly a bit better solution compared to the accepted answer: from better_abc import ABCMeta, abstract_attribute # see below class AbstractFoo(metaclass=ABCMeta): @abstract_attribute def bar(self): pass class Foo(AbstractFoo): def __init__(self): self.bar = 3 class BadFoo(AbstractFoo): def __init__(self): pass It will behave like this: Foo() # ok BadFoo() # will raise: NotImplementedError: Can’t instantiate abstract class BadFoo … Read more

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

For the interface, the addition of the abstract, or even the public keywords would be redundant, so you omit them: interface MyInterface { void Method(); } In the CIL, the method is marked virtual and abstract. (Note that Java allows interface members to be declared public abstract). For the implementing class, there are some options: … Read more

Abstract Method Error

java.lang.AbstractMethodError is thrown when an application tries to call an abstract method. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled. Seems like this problem is due to version incompatibility in … Read more

Can we instantiate an abstract class directly? [duplicate]

You can’t directly instantiate an abstract class, but you can create an anonymous class when there is no concrete class: public class AbstractTest { public static void main(final String… args) { final Printer p = new Printer() { void printSomethingOther() { System.out.println(“other”); } @Override public void print() { super.print(); System.out.println(“world”); printSomethingOther(); // works fine } … Read more

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

This code demonstrates why you should never call an abstract method, or any other overridable method, from a constructor: abstract class Super { Super() { doSubStuff(); } abstract void doSubStuff(); } class Sub extends Super { String s = “Hello world”; void doSubStuff() { System.out.println(s); } } public static void main(String[] args) { new Sub(); … Read more

Extend data class in Kotlin

The truth is: data classes do not play too well with inheritance. We are considering prohibiting or severely restricting inheritance of data classes. For example, it’s known that there’s no way to implement equals() correctly in a hierarchy on non-abstract classes. So, all I can offer: don’t use inheritance with data classes.