• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Superclass has subclass reference

 
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a situation where I have to put getters and setters for a sublass reference in superclass. I just need to know whether its a good practice or not?

I have given the code below.



Thanks in advance.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That entirely depends on the actual business requirements. Nothing useful can be said about classes with meaningless names like "SuperClass".
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Paul that you cannot say in general if this is good or bad practice - it depends on the business requirements and design of the system.

At first sight though it looks a little strange, because this introduces a tight coupling between the superclass and the subclass: the superclass has to know that the subclass exists. Normally superclasses don't contain direct knowledge about the subclasses that might exist.

I'm curious to know what the real use case for such a construction is. Can you explain us why it's designed like this in your program?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Jesper: a class knowing about specific subclasses is usually (possibly even "always") a poor design. I strongly suspect there's a better approach.
 
Bartender
Posts: 10759
68
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumukh Deshpande wrote:I have a situation where I have to put getters and setters for a sublass reference in superclass...


I'd go a bit further than the others and say that it doesn't make much sense to me - certainly the "setter" part. The "getter" part is, however, possible with generics, viz:No concrete class should ever need to extend it directly, but it could be useful for "skeleton" classes that need to return properly typed instances, because it saves an explicit cast operation.

However, I can't think of any reason why you would need a subclass setter in a superclass. Maybe you could explain why you think you need it?

Winston
 
Sumukh Deshpande
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Sumukh Deshpande wrote:I have a situation where I have to put getters and setters for a sublass reference in superclass...


I'd go a bit further than the others and say that it doesn't make much sense to me - certainly the "setter" part. The "getter" part is, however, possible with generics, viz:No concrete class should ever need to extend it directly, but it could be useful for "skeleton" classes that need to return properly typed instances, because it saves an explicit cast operation.

However, I can't think of any reason why you would need a subclass setter in a superclass. Maybe you could explain why you think you need it?

Winston





The reason behind designing that way :
I have a scenario in developing a Web Service using Apache Axis 2.

The rule is only the classes referred in Web Service are shown to the client. So now when I just say SubClass extends SuperClass then the SubClass is not actually shown to the user.

If at all it is to be shown then in some way SuperClass needs to refer it so I introduces a getter and setter for SubClass in SuperClass.

I will show it by an example:



Now the client would do some thing like this:



It is very natural that createCar would take the argument as SuperClass for inheritance advantage as user can create any type for car which is in the inheritance hierarchy and pass it to the function.

As the functions accept / return only super class client will not be able to see the SubClass as it is not referred.

So either I have to write the function for all type of cars or give getters and setters in SuperClass.

I hope it explains something.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of thing can happen in the Factory method pattern.

However given your opaque class names, it's impossible to tell what your design is all about. I can't tell whether it's a factory pattern or not. I bet it isn't about cars either. If you have a specific design you want to ask about, then ask about it.
 
Winston Gutkowski
Bartender
Posts: 10759
68
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumukh Deshpande wrote:The reason behind designing that way :
I have a scenario in developing a Web Service using Apache Axis 2.

The rule is only the classes referred in Web Service are shown to the client. So now when I just say SubClass extends SuperClass then the SubClass is not actually shown to the user.

Erm...okay.

If at all it is to be shown then in some way SuperClass needs to refer it so I introduces a getter and setter for SubClass in SuperClass.

I will show it by an example:...

Now I'm totally confused.
You keep talking about 'getters' and 'setters', but all I see is a retrieval method and a method that creates a new object.

A subclass isn't contained in it's superclass; it IS its superclass (or, more specifically, a specialization of it).
What you appear to want is some way of casting a subclass to its superclass; and that's completely different from what you described.
Either that, or I've got it hopelessly wrong.

Presuming that this "referral" is declarative in some way, why not just declare all the subclasses that may be needed by the Web service?

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!