Question about inheritance


How to implemment this simplest Java code in kotlin? Can this be done on the Kotlin what can be done in java?

class Grammar01 {   static void method01() {      } } class Grammar02 extends Grammar01 {      static void method02() {   } } class Grammar03 extends Grammar02 {   static void method03() {   method01();   } }


This code solves the problem. But it is very verbose solution.

public abstract class Grammar01Factory {   public fun method01() {   } } open public class Grammar01 {   open public class object: Grammar01Factory() {   } } public abstract class Grammar02Factory: Grammar01Factory() {   fun method02() {   } } open public class Grammar02 {   open public class object: Grammar02Factory() {   } } public abstract class Grammar03Factory: Grammar02Factory() {   fun method03() {   method01()   } } open public class Grammar03 {   open public class object: Grammar03Factory() {   } } fun test() {   Grammar03.method01()   Grammar03.method02()   Grammar03.method03() }

This is the only way?
Or are there other options?


"Static" things are not inherited in Kotlin. Inheritance does not make much sense in your example.

It seems that what you are looking for is calling mathod01() by its short name.

You can use imports for that:

// grammar01.kt
package grammar01

fun method01() {}

one more file:


// grammar03.kt
package grammar03

import grammar01.*

fun method03() {

Classes are needed to instantiate them. If all you need is a bunch of functions, packages work just fine.


The existing classes are more complex and they are static.

class Grammar {   static public Rule matchString(String s) {   return new Rule();   }   static public Rule matchChar(Character c) {   return new Rule();   }   static public void initGrammar(Class<?> clazz) {   // init using reflection   } } class SharedGrammar extends Grammar {   public static Rule function = matchString("def");   public static Rule comma = matchChar(',');   static {   initGrammar(SharedGrammar.class);   } } // inherit and extends and override some fields and properties class BasicGrammar extends SharedGrammar {   // override SharedGrammar   public static Rule function = matchString("fun");   public static Rule equals = matchString("==");   static {   initGrammar(BasicGrammar.class);   } } // inherit and extends and override some fields and properties class ConcreteGrammar extends BasicGrammar {   // override BasicGrammar   public static Rule equals = matchString("===");   static {   initGrammar( ConcreteGrammar.class);   } }

It is possible, of course, be rewritten to use dynamic classes.
But it will not quite what it actually is.
And the practice is not always possible to change something.

Features of the language does not always allow him to be back-end (parent classes) for Java. Rather, it is front-end. Or equal, but not fully compatible with Java. Or, even better than Java, but not replace Java completely.