• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design pattern for more methods in subclass than in parent

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a scenario like this





Now in my caller i am wanting to create full C or B objects


Now no matter what i do i.e A a = new A or A A = new b|c, i cant access b or c elements from parent's reference...obviously!.
This tells me that there is some inherent design flaw in the way classes are being organized. Is there a way to design this..any design pattern for this kind of scenario where 90% of the members exist in parent class and a complete object including child elements is to be created

Kindly suggest





 
Rancher
Posts: 436
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to understand the difference between an object and a reference.



defines a reference variable on the stack.

creates an A object on the heap.

assigns the object to the reference.

Every manipulation you do you do via the reference variable. The type of the reference variable defines the set of methods that you can use. You can't use B's or C's methods on an A.

Every manipulation you do is done to the object. An A is not an B or an C. You will never be able to use B's or C's methods on an A.

You can assign subclasses of a type to a variable of a supertype. You can assign B or C to a reference variable of type A.

You shouldn't set all those values externally but inside the constructor of the subclass.

Your code should look like:



If you absolutely must modify the object afterwards either think of a specifically typed variable:



or you can cast:



But a setup of an object outside the constructor is fishy in itself.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create an object in your caller class as:
or
If you want to access B or C classes public members or methods, you need to cast your reference variable "a" which is of type A initially.

So, you need to write something like
or

A class members you can access directly as you "a" is of type A.
 
sandeeprajsingh tandon
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your inputs.
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case though, going by the field names I would say that inheritance is the wrong tool for the job regardless.

It seems you have a Person class, and you want to add more properties by extending it. Usually, doing this with roles is a much better idea.
reply
    Bookmark Topic Watch Topic
  • New Topic