Middle
What is the main difference between Java and Kotlin regarding abstract classes and methods?
sobes.tech AI
Answer from AI
In Java, an abstract class can contain both abstract and non-abstract methods. Abstract methods do not have an implementation, while non-abstract methods do. Abstract classes cannot be instantiated directly.
In Kotlin, the concept is very similar. The key difference is that in Kotlin, all classes are final by default. To make a class abstract, it must be explicitly marked with the abstract keyword. Like in Java, abstract methods do not have an implementation, while non-abstract methods in an abstract class can have an implementation.
abstract class MyAbstractKotlinClass { // Abstract class in Kotlin
abstract fun abstractMethod(): String // Abstract method (without implementation)
fun concreteMethod(): Int { // Non-abstract method (with implementation)
return 42
}
}
abstract class MyAbstractJavaClass { // Abstract class in Java
public abstract String abstractMethod(); // Abstract method (without implementation)
public int concreteMethod() { // Non-abstract method (with implementation)
return 42;
}
}