Widget extended Class

Hello,

I have 2 classes with A extends B and B is overwriting a function of A.
I’m writing a widget that has a function with an instance of A as parameter.
Now I want to call the function with an instance of B so it gets automatically casted to type A.

shouldn’t I be able to call the function of B? or is there a way to cast it back dynamically?

thx

This is a pure Java question, not related to Vaadin.

If you google for “java inheritance basics” you’ll find good explanations on how such things work (is-a relationship, …).

I dont think that you got my problem.

It’s something like this:


class A{
	void speak(){
		System.out.println("this is A");
	}
}
class B extends A{
	@Override
	void speak(){
		System.out.println("this is B");
	}
}

void main() {
	fn(new B());
}

static void fn(A item){
	item.speak();
}

should print “this is B”, right?

but if you create an widget and send an instance of B as param of fn it would print “this is A” in the widget

and instanceof B is false

That still does not have anything to do with Vaadin, it is still basic Java inheritence knowledge which google has plenty of links to help you with.

Just follow Henri’s advice and you’ll figure that out.

One very, very vague guess (although 90% of the relevant information is missing so just guessing what you mean by “widget” here): are you talking about RPC here rather than local method calls?

For RPC and shared state, serialization and deserialization uses declared types only, not the actual instance type.

Yes, used the RPCs like in the tutorials for own widget creation (with ClientRpc, Connector… etc.)
Is there any way to send it with the actual instance type?