This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Design pattern for more methods in subclass than in parent

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Sheriff
Posts: 7708
534
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you for your inputs.
 
Saloon Keeper
Posts: 12254
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic