For a interface such as
public interface something<T>
{
public something<T> somemethod();
}
from what i understand the abstract method somemethod() needs to overridden with a method that returns an object implementing the interface. However, any attempts to do so have given me the "does not override the abstract method somemethod()" compiler error.
I've tried doing something like
public class someclass {
...
public something<T> somemethod() { ... return new someclass(); }
...
or
public someclass somemethod() { ... return new someclass(); }
...
}
How exactly would i implement such a method?
You're missing the generic declaration in your implementing class. Here's an example:
public interface Something <T> {
public Something<T> someMethod();
}
class SomethingImplementation <T> implements Something <T> {
#Override
public Something<T> someMethod() {
return null;
}
}
All of these should compile:
class test<T> implements something<T>{
public something<T> somemethod(){
return new test<T>();
}
}
class test1<T> implements something<T>{
public test1<T> somemethod(){
return new test1<T>();
}
}
class test2 implements something<Integer>{
public something<Integer> somemethod(){
return new test2();
}
}
class test3 implements something<Integer>{
public test3 somemethod(){
return new test3();
}
}
First of all, your someclass does not implement the interface in your code example above. You can make it implement the interface for a specific, concrete type, as in the following example, where String is used as the concrete type. The method then would need to return a something<String>.
public class someclass implements something<String> {
public something<String> somemethod() {
return new someclass();
}
}
Or you could have class someclass have a type parameter and use that for the interface:
public class someclass<X> implements something<X> {
public something<X> somemethod() {
return new someclass<X>();
}
}
from what i understand the abstract method somemethod() needs to
overridden with a method that returns an object implementing the
interface.
That is not correct. The method somemethod needs to return a something<T>.
You also need to extend the interface with a generic, such as:
public class someclass implements something {
public something somemethod() {
...
}
}
To override a method you have to implement a method with the same types of paramenters and the same type of return value.
String something(){} will be overriden by String something(){}
but NOT with char[] something(){} or String something(int){}
As I understand, you need to implement interface something, You can do this in simple way:
public class someclass<T> implements something<T> {
public something<T> somemethod() { ... return new someclass(); }
}
public class someclass<T> extends something<T> {
public something<T> somemethod() { ... return new someclass<T>(); }}
Related
I‘m quite struggling by defining an abstract method in an interface. Why? In each class, related to this interface one method called “getInstance” has to be implemented. These methods returns the actual used instance.
So how do I define the abstract method in an interface for different classes?
public interface MyInterface {
public <<here is my question>> getInstance();
}
public class Class1() implements MyInterface {
public Class1 getInstance() {
return this;
}
}
public class Class2() implements MyInterface {
public Class2 getInstance() {
return this;
}
}
Please see my comment, it seems unlikely you really want to do this.
You can do what Khan Saab pointed out: Have getInstance's return type be MyInterface. But that won't work if you need to access features of Class1 or Class2 that aren't part of the interface.
You can do something like this with generics:
public interface MyInterface<T> {
public T getInstance();
}
public class Class1 implements MyInterface<Class1> {
public Class1 getInstance() {
return this;
}
}
public class Class2 implements MyInterface<Class2> {
public Class2 getInstance() {
return this;
}
}
But you couldn't (for instance) have an array of MyInterface and use a mix of Class1 and Class2 in it (without instanceof and casting).
All of which probably means you want to do something else.
I think you might find the idea of Generics useful:
For example:
Interface
public interface TestWithGenerics<T> {
public T getInstance();
}
Implementation
public class TestImplementation implements TestWithGenerics<TestImplementation> {
#Override
public TestImplementation getInstance() {
return this;
}
}
However, as the comments said, this all feels a bit funny. I find singletons to be a code smell. But I'm not sure what your requirements are, and I hope this helps.
As both the classes implement the interface MyInterface hence object of each class will be of type MyInterface.
public interface MyInterface{
public MyInterface getInstance();
// ....other methods
}
public class Class1 implements MyInterface{
#override
public MyInterface getInstance(){
return this;
}
//... other code
}
public class Class2 implements MyInterface{
#override
public MyInterface getInstance(){
return this;
}
//... other code
}
awesome :-)
finally I implemented the solution from “T.J. Crowder”. It works fine. Many many thanks to all of you. You gave me very interested hints on my road to create “better” code……
Here's My code:
public interface Baseinterface {}
abstract class Interface1 implements Baseinterface{}
abstract class Interface2 implements Baseinterface{}
public interface Classinterface {}
And i want to use this code:
public class Myclass(Baseinterface interfaceversion) implements Classinterface{}
Where the kind of interface implementation is passed as a constructor.
So when a function is defined in both of those abstract classes my actual class knows which one to use. I am fairly new at java.
Thanks.
I may be misunderstanding the nature of the question, but here goes:
Given this code which describes two abstract classes that implement the same method as defined by an interface:
interface BaseInterface {
void foo();
}
abstract class ITestA implements BaseInterface {
public void foo() {
System.out.print("A");
}
}
abstract class ITestB implements BaseInterface {
public void foo() {
System.out.print("B");
}
}
public class MyClass {
private BaseInterface enclosed;
MyClass(BaseInterface base) {
enclosed = base;
}
public void foo() {
enclosed.foo(); // call the implementation specific to the instance passed in by constructor
}
}
This could be called like:
public class Test {
void bar() {
// This may look weird cause we're defining an anonymous implementation of the abstract class, without adding any new implementation details
ITestA impl = new ITestA() {};
MyClass myClass = new MyClass(impl);
myClass.foo(); // prints "A"
}
}
public interface foo {
String ex(String a);
}
public class myclass implements foo {
public String ex(String a) {
//define the method
return a;
}
public static foo getsome() {
//have to return for example if I do ex("abc") return "123" but have to retrun the object of the interface o.O
}
}
I don't know how to return an object of an Interface because I know that an object of interface cannot be implemented. On the other side, get methods of the all commands has no input. So what can I do?
I will call object, an instance of a class, for example:
private myclass myObject = new myclass();
This object (myObject) can be accesses as its class (myclass), any interface it implements (foo) or any class it extends (Object, because every class extends Object in Java). So the following are all valid:
public myclass getMyClass() { return myObject; }
public foo getMyFoo() { return myObject; }
public Object getMyObject() { return myObject; }
So coming back to your code, if you want to use a static method:
private static myclass instance = new myclass();
public static foo getsome() {
return instance;
}
Assume I have
public interface MyInterface {
public void aMethod();
}
public class MyClass implements MyInterface {
#Override
public void aMethod() {
Log.d("aMethod");
}
}
And I want to declare an object that implements MyInterface, so I can instantiate it in the constructor as follows
public class AnotherClass {
Class <? extends MyInterface> mObjectThatImplements;
public AnotherClass() {
// Says it cannot convert MyClass to Class <? extends MyInterface>
mObjectThatImplements = new MyClass();
}
}
You just change the declaration type. Make it as a Interface, and you can assign any implementation to it.
public class AnotherClass {
MyInterface mObjectThatImplements;
public AnotherClass() {
mObjectThatImplements = new MyClass();
}
}
Say you have a super-class. In that super class you want to pass runtime object of itself (this) as a parameter to an overloaded method. Trick is, this overloaded method is overloaded by sub-class type. When you try to do it, you'll get a
method ... is not applicable(actual argument
... cannot be converted to ... by method invocation
conversion)
Instead you would need to implement the method separately in each subtype (just to get the correct runtime class), which is a lot of duplicate effort when the contents of the method are identical.
e.g:
public class InferTypeTest {
public static void main(String[] args) {
SubClass1 s1 = new SubClass1();
s1.sayHi();
}
public static void sayHi(SubClass1 clz) {
System.out.println("clz 1");
}
private abstract static class SuperClass{
public void sayHi() {
InferTypeTest.sayHi(this);
}
}
private static class SubClass1 extends SuperClass{
}
}
Yes, this is how double dispatch works, you have to override the accept method in each subclass like this:
private static abstract class NodeWithChildren implements DomNode {
/* snip */
public void accept(DomNodeVisitor visitor) {
for (DomNode child : children) {
child.accept(visitor);
}
}
}
private static class BodyNode extends NodeWithChildren {
public void accept(DomNodeVisitor visitor) {
visitor.visit(this);
super.accept(visitor);
visitor.visited(this);
}
}
private static class DivNode extends NodeWithChildren {
public void accept(DomNodeVisitor visitor) {
visitor.visit(this);
super.accept(visitor);
visitor.visited(this);
}
}
}
BodyNode is a NodeWithChildren
DivNode is a NodeWithChidren
NodeWithChildren is a DomNode
DomPrinterVisitor is a DomNodeVisitor
DomNodeVisitor's visitor can visit "BodyNode" or "DivNode, But you are passing "NodeWithChildren" to visit.
Here BodyNode is a NodeWithChildren but NodeWithChildren is not BodyNode/
Theary is if B extends A, you can say B is a A/ not A is a B.