Home python how to replace multiple inheritance in java?

how to replace multiple inheritance in java?

Author

Date

Category

I want to implement approximately the following architecture:

Explanation:

  • Base – something like a storage entity in which some basic objects are stored;
  • Childn – descendants that implement their spectrum of action on the data that are stored in the class BASE . Essentially interfaces, because No state stored;
  • Static Mixin – class combining all methods from Childn . The collisions between the names of the methods are not. All its methods (i.e., the methods inherited from Childn ) must be static, causeless without instance.

in Python This task would be easy to solve Multiple inheritance, in java note is not. How to be?


Approximate pseudo-code what I want to implement:

class base {
  Static Protected Final String SomeString = "01";
}
Class Child1 EXTENDS BASE {
  STATIC PUBLIC CHAR GET_0 () {
    Return SomeString.Charat (0);
  }
}
Class Child2 EXTENDS BASE {
  STATIC PUBLIC CHAR GET_1 () {
    Return SomeString.Charat (1);
  }
}
Class Mixin Extends Child1, Child2 {// so can not do
}
Public Class Main {
  Public Static Void Main (String [] Args) {
    System.out.PrintLN (Mixin.get_0 ()); // Total use should be
    System.out.PrintLN (Mixin.get_1 ());
  }
}

Answer 1, Authority 100%

As it turned out, the separation of the functional into several classes with a re-association in one you need to be solely for the purposes of logical grouping. Possible solutions to your problem:


Using interfaces

You can make Child, interfaces. In Java, there is no multiple inheritance, but you can implement multiple interfaces. To make the implementation of methods directly in the interfaces (so that they do not require implementing in their class) , you can use the default keyword :

class base {
  Static Public Final String Str = "01";
}
Interface Child1 {
  DEFAULT CHAR GET0 () {
    Return Base.str.Charat (0);
  }
}
Interface Child2 {
  DEFAULT CHAR GET1 () {
    Return Base.str.Charat (1);
  }
}
Class Mixin Implements Child1, Child2 {
}
Class Main {
  Public Static Void Main (String [] Args) {
    Mixin Mixin = NEW Mixin ();
    System.out.PrintLN (Mixin.get0 ());
    System.out.PrintLN (Mixin.get1 ());
  }
}

Refusal to split the functionality

You work with one BASE repository. In my opinion, there is more than enough use of the same class without any interfaces. Moreover, you can accommodate all these methods in the Base class.

For example:

class base {
  PRIVATE FINAL STRING STR = "01";
  Public String Getstr () {
    Return str;
  }
}
Class Mixin {
  Private Base Base;
  Public Mixin (Base Base) {
    this.Base = Base;
  }
  Public Char Get0 () {
    Return base.getstr (). Charat (0);
  }
  Public Char Get1 () {
    Return base.getstr (). Charat (1);
  }
}
Public Class Main {
  Public Static Void Main (String [] Args) {
    Mixin Mixin = NEW Mixin (New Base ());
    System.out.PrintLN (Mixin.get0 ());
    System.out.PrintLN (Mixin.get1 ());
  }
}

Using static internal classes

You say you need a logical grouping. In Java, there is a design that is called a static internal or invested class. Inside the class, another class is created, which is noted Static . So you can achieve a logical group. Example:

class Base {
   static public final String someString = "01";
}
class MixIn {
   public static class Child1 {
     static char get1 () {
       return Base.someString.charAt (1);
     }
   }
   public static class Child2 {
     static char get0 () {
       return Base.someString.charAt (0);
     }
   }
}

See, I created a generic MixIn class, and inside it are two static classes. They are called like this:

class Main {
   public static void main (String [] args) {
     System.out.println (MixIn.Child1.get1 ());
     System.out.println (MixIn.Child2.get0 ());
   }
}

As you can see, there is no instantiation, and the classes / methods are logically grouped.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions