JAVA - Problems with Override on Interface and Class - java

Salve...
some Problem for me in Java. I have generated an Interface and an Class implements this Interface. In the Interface i have declared one Method... and in the Class i will Override this. But not works! But don`t why....
See my example Interface:
public interface IMyClass extends IInterfaceA<IInterfaceB> {
public List<IInterfaceB> getMethod(Integer id);
}
See now my Class File:
public class MyClass implements IMyClass {
#Override
public List<IInterfaceB> getMethod(Integer id) {
return anything;
}
}
Problem is... i think i make no mistake. And next - i surprise that i have another Interfaces and Classes with the same Logic - and it will work and make no Problems!
In my another Interfaces & Classes i don`t need to make the Class with implements the Interface an Abstract Class.
When i make an build... Jenkins will surprise me with this Message:
[ERROR] COMPILATION ERROR :
error: MyClass is not abstract and does not override abstract method getMethode() in IMyClass [ERROR]
error: method does not override or implement a method from a supertype [INFO] 2 errors [INFO]

Solved . The " solution " - was a build problem . After tens of Maven Clean Installs and re- builds he has the connection interface and class savvy and it goes... oh men ;)

Related

Java Generic Interface 'Type Argument Not Within Bounds' Error

I seems to be running into a compilation error with a generic interface. The goal is to create an interface for use in my test classes the defines the common methods of a domain class and a generated DTO class. I did some digging and none of the answers I have found so far seem applicable to what I am trying to do. Most of the answers either refer back to this: class not within type bounds or they suggest using wildcards which is not an option here.
When I try to compile, I get the following error:
[uberCompile] BrandDto.java:10: error: type argument T#1 is not within bounds of type-variable T#2
[uberCompile] public class BrandDto<T extends SubCollectionDto> implements IBrand<T> {
[uberCompile]
[uberCompile] where T#1,T#2 are type-variables:
[uberCompile] T#1 extends SubCollectionDto declared in class BrandDto
[uberCompile] T#2 extends ISubCollection declared in interface IBrand
My class structure looks like this:
interface IBrand<T extends ISubCollection>
interface ISubCollection
class Brand<T extends SubCollection>
extends Entity
implements IBrand<T>
class SubCollection
extends Entity
implements ISubCollection
The generated DTO classes look like:
class SubCollectionDto
implements ISubCollection
class BrandDto<T extends SubCollectionDto>
implements IBrand<T>
I'm really racking my brain trying to understand what I am doing wrong here. Any help would be greatly appreciated.
I think I finally figured it out. It was actually a classpath issue that had nothing to do with the generics. When I originally set up the DTO classes they were under the 'src' directory. I later moved them into a different directory, but apparently the old folder never got removed. So eclipse was looking at the current, correct set of files while ant was looking at the old directory. No idea why that was causing the error message I saw, but it is working now.

JavaEE: Custom class in a EJB remote interface

I want to add my class as an argument in the stateless, remote interface. For example:
In my remote interface I have this method
void connect(MyClass myClass);
But I didn't import MyClass (which is a Java Class library) in my interface, so I got an error. Is there any workaround?
In my SessionRemote class I imported MyClass library. Is there any chance to override that method in SessionRemote without importing MyClass in the interface?
#Override
public void connect(MyClass myClass){
myClass.foo();
}
I tried to add MyClass library to the interface, but I got lots of errors.

Is it possible for an interface to be accessible only in the same package and child packages?

Is it possible for an interface to be accessible only in the same package and child packages?
I have defined an interface with default modifier:
package com.mycompany.myapp.dao;
import java.io.Serializable;
interface BaseDao<T, Id extends Serializable> {
public void create(T t);
public T readById(Id id);
public void update(T t);
public void delete(T t);
}
Now I have a child package where I want to define a class which implements BaseDao. So I wrote this code:
package com.mycompany.myapp.dao.jpa;
import java.io.Serializable;
public class BaseDaoJpa<T, Id extends Serializable> implements BaseDao<T, Id> {
...
}
But I get this error:
BaseDao cannot be resolved to a type
So is this a restriction from Java for an interface or am I doing it wrong way?
Thanks
In Java there is no such thing as a "child package". Don't get fooled by the dots. com.mycompany.myapp.dao and com.mycompany.myapp.dao.jpa are two separate packages which have no relation to each other.
So to answer your question: no, it is not possible to make an interface visible only to child packages. You can make your interface public, but then it will be visible to all other packages.
Have a look on Java Acess Modifiers:
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
In the table you can see that default or No modifier is limited to acess only by the same class or other classes in the same package. As i understand you want it to be visible to other packages as well but not to the world, and for that you need to use protected modifier, but it is impossible since it is not aplicable so going back to your question no you cant
:- (
If you are looking for some way to hide and exposes only what you want to the rest of your Java application, maybe you want a component so take a look at OSGi. This question is a good start to read before jumping (or not) into it.
It looks like you have extends where you need implements.
public class BaseDaoJpa<T, Id extends Serializable> implements BaseDao<T, Id>
{
...
}

Strange behaviour extending abstract class (exercise)

I'm studying for Java SE 7 certification exam and I'm doing some boring excercises about inheritance and access modifiers.
But now I'm getting an unexpected behavior applying inheritance. In my base package com.testpkg I have an abstract class:
package com.testpkg;
public abstract class Abstract {
public int test();
}
NOTE that I voluntarily omitted the abstract modifier for the test() method.
Then I have a concrete class extending Abstract in package com.testpkg.sub:
package com.testpkg.sub;
public class Concrete extends Abstract {
public int test() {
return 0;
}
}
I test this classes using:
package com.testpkg;
import com.testpkg.sub.Concrete;
public class TestMain {
public static void main(String[] args) {
Abstract c = new Concrete();
System.out.println(c.test());
}
}
If I try to compile this, I obviously get two errors:
The method test() requires a body instead of a semicolon
But if I run test class: 0 is printed on console!
This seems very strange to me. Can you explain why the code is working even test() is wrongly declared in Abstract class?
NOTE I'm using Eclipse IDE to compile/run my code.
You need an abstract qualifier on your test() method. You're likely running an old class file. If you change the value of your return statement to something besides 0, you will see that it isn't running this code.
Eclipse allows you to run "half baked" code. The reason why it runs fine here is because, at runtime, you never really depend on the specifics of the Abstract class so it doesn't really matter if you have this type of compile error in it.
Yours class Abstract has compilation errors, because non abstract method should be defined, so it should have a body. abstract qualifier is optional only in case of interfaces.

Problem in html to java class parsing using XMLC

i have compiled HTML page using XMLC jar file command..
java org.enhydra.xml.xmlc.commands.xmlc.XMLC -d \core\model\template -class core.model.template.patientPrescriptionsHTML \\core\\model\\template\\PrePrintPrescription.html
it propogate error like
patientPrescriptionsHTML.java:17: core.model.template.patientPrescript
ionsHTML is not abstract and does not override abstract method renameNode(org.w3
c.dom.Node,java.lang.String,java.lang.String) in org.w3c.dom.Document
public class patientPrescriptionsHTML extends org.enhydra.xml.xmlc.html.HTMLObje
ctImpl implements org.enhydra.xml.xmlc.XMLObject, org.enhydra.xml.xmlc.html.HTML
Object {
^
1 error
Error: compile of generated java code failed
please suggest...
In
class A extends B implements C {
}
A must implement all abstract methods from B and C (because C is an interface, all of C's methods count implicitly as abstract.)
Unless, of course, A itself is abstract.

Categories

Resources