Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java partial classes

Small preamble. I was good java developer on 1.4 jdk. After it I have switched to another platforms, but here I come with problem so question is strongly about jdk 1.6 (or higher :) ). I have 3 coupled class, the nature of coupling concerned with native methods. Bellow is example of this 3 class

public interface A
{
     public void method();
}
final class AOperations
{
     static native method(. . .);
}
public class AImpl implements A
{
    @Override
    public void method(){ 
        AOperations.method( . . . );
    }
}

So there is interface A, that is implemented in native way by AOperations, and AImpl just delegates method call to native methods. These relations are auto-generated. Everything ok, but I have stand before problem. Sometime interface like A need expose iterator capability. I can affect interface, but cannot change implementation (AImpl).

Saying in C# I could be able resolve problem by simple partial: (C# sample)

partial class AImpl{
 ... //here comes auto generated code
} 

partial class AImpl{
 ... //here comes MY implementation of 
 ... //Iterator 
} 

So, has java analogue of partial or something like.

EDITED: According to comment by @pgras I need some clarification. AImpl is not in vacuum, there is some factory (native implemented) that returns instance of AImpl, that is why creation of inheritance from AImpl, is not applicable.

EDITED 2: May be it doesn't relate, but how it is done by JUnit 4:

public class SomeTest {
 ...
 //there is no direct inheritance from Assert, but I can use follow:
 assertTrue(1==1); //HOW DOES it works??
like image 742
Dewfy Avatar asked Mar 23 '10 16:03

Dewfy


People also ask

What are partial classes in Java?

A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler.

Are there partial classes in Java?

When we look at Java, they do not have such kind of RAD approach. So there are no partial classes, no code behind, etc.

What are the partial classes?

Partial Class is a unique feature of C#. It can break the functionality of a single class into many files. When the application is compiled, these files are then reassembled into a single class file. The partial keyword is used to build a partial class.

What is the point of a partial class?

Partial classes are portions of a class that the compiler can combine to form a complete class. Although you could define two or more partial classes within the same file, the general purpose of a partial class is to allow the splitting of a class definition across multiple files.


2 Answers

Java does not have support for partials or open classes. Other JVM languages do, but not Java. In your example, the simplest thing may unfortunately be to use delegation. You can have your AImpl take another object that fulfills an interface to these extension methods. The generated AImpl would then have generated methods such as iterator methods that it could delegate to the user created object you pass in.

like image 186
Russell Leggett Avatar answered Nov 11 '22 03:11

Russell Leggett


How about that: 
Compute.java  =    your class
Compute$.java  =   base class for partial classes. Reference a Compute object
Compute$Add.java = your partial class. Subclass Compute$.
Compute$Sub.java = your partial class. Subclass Compute$.

file Compute.java

public class Compute {
    protected int a, b;
    Compute$Add add;
    Compute$Sub sub;

    public Compute() {
        add = new Compute$Add(this);
        sub = new Compute$Sub(this);
    }

    public int[] doMaths() {
        int radd = add.add();
        int rsub = sub.sub();
        return new int[] { radd, rsub };
    }
}

file Compute$.java

public abstract class Compute$ {
    protected Compute $that;
    public Compute$(Compute c){
        $that=c;
    }
}

file Compute$Add.java

public class Compute$Add extends Compute$ {
    public Compute$Add(Compute c) {
        super(c);
        // TODO Auto-generated constructor stub
    }

    public int add(){
        return $that.a+$that.b;
    }
}

file Compute$Sub.java

public class Compute$Sub extends Compute$ {
    public Compute$Sub(Compute c) {
        super(c);
    }

    public int sub() {
        return $that.a - $that.b;
    }
}
like image 38
Max Mba Avatar answered Nov 11 '22 03:11

Max Mba