Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to make Java parent class method return object of child class

Tags:

java

generics

Is there any elegant way to make Java method located within parent class return object of child class, when this method is called from child class object?

I want to implement this without using additional interfaces and extra methods, and to use this without class casts, auxiliary arguments and so on.

Update:

Sorry that I was not so clear.

I want to implement method chaining, but I have problems with methods of parent class: I lose access to child class methods, when i call parent class methods... I suppose that I'v presented the core of my idea.

So the methods should return this object of this.getClass() class.

like image 359
Timofey Gorshkov Avatar asked Oct 27 '10 09:10

Timofey Gorshkov


People also ask

Can parent class create object of child class?

In Simple Terms, Objects of Parent class can hold objects of child class.

How do you call a method of parent class by an object of a child class in Java?

If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method. This would print: I'm the child.

Can I access a parent class method by a child class object?

The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class.

How do you get parent object from child object?

if you have B b = new B(); you can get the parent A by default casting Java provides. A a = (A)b This is automatic, so just writing A a = b will have the same result.


2 Answers

If you're just looking for method chaining against a defined subclass, then the following should work:

public class Parent<T> {    public T example() {     System.out.println(this.getClass().getCanonicalName());     return (T)this;   } } 

which could be abstract if you like, then some child objects that specify the generic return type (this means that you can't access childBMethod from ChildA):

public class ChildA extends Parent<ChildA> {    public ChildA childAMethod() {     System.out.println(this.getClass().getCanonicalName());     return this;   } }  public class ChildB extends Parent<ChildB> {    public ChildB childBMethod() {     return this;   } } 

and then you use it like this

public class Main {    public static void main(String[] args) {     ChildA childA = new ChildA();     ChildB childB = new ChildB();      childA.example().childAMethod().example();     childB.example().childBMethod().example();   } } 

the output will be

org.example.inheritance.ChildA  org.example.inheritance.ChildA  org.example.inheritance.ChildA  org.example.inheritance.ChildB  org.example.inheritance.ChildB 
like image 166
Gary Rowe Avatar answered Sep 23 '22 09:09

Gary Rowe


What are you trying to achieve ? It sounds like a bad idea. A parent class should not know anything about its children. It seems awfully close to breaking the Liskov Substitution Principle. My feeling is that your use case would be better serve by changing the general design, but hard to say without more informations.

Sorry to sound a bit pedantic, but I get a bit scared when I read such question.

like image 35
Guillaume Avatar answered Sep 21 '22 09:09

Guillaume