aspose file tools*
The moose likes Java in General and the fly likes running inheritance backwards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "running inheritance backwards" Watch "running inheritance backwards" New topic
Author

running inheritance backwards

Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
<code>


I think that this is not upcast, and not downcast. As I see, I'm casting Z to Z only becouse it is happening in Q.

Is it possible to make a programmatic casting, so, for example to have: ((somehowVariable)this).andThenSomethingStrange() .... ?
This is probabbly a wrong question, with so many answers (like reflections), but I'm thinking how to do somehting like this, and this question is just becaouse i am so unexperienced..I have some wishes that I can not say what they are..

The truth is that I made this by experimenting, quite accedentaly, and I don't know what to think about it (the whole program or design), but I like it becaouse like this I can call methods in subclass from superclass. I call it for now "telescopic eye", but I don't know what is this...

What do you think about this small piece of code?

Like, I imagine it like a tower with "lift" from which I can choose to call whatever method from which ever level I wish, not even konwing which methods will be in subclasses... like , for example, subclasses could be made like that, that.. after somehow root class gathers all the methods, it also gathers some information how to call them, or somwhere in subclasses there are some information about that. This is kind of without a purpouse, just imagination.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
A subclass object IS-A superclass object.

In this case you are running inheritance backwards, testing whether a superclass object IS-A subclass object. It might be permissible syntax, but it looks like very peculiar design to me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
And don't use thread titles like "qestion about small piece of code"
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

I agree that looks strange.

Firstly...neither class is actually inheriting from the other. There's no extends used anywhere. So line 17 isn't going to compile.

If that's just an oversight, and Z extends Q, then it's not good practice to have Q checking which particular subclass it is. Classes shouldn't know anything about other classes that extend them, it's up to the extending class to provide any special behaviour. Otherwise you need to modify the base class every time you add a new subclass.
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Thank You thank you....
These are my exact questions.... running inheritance backwards.... that was exactelly what I wanted to know how to call it..... I am qute aware that instanceof tests like this are not really good thing, and I even know that this is called a "violation of Liskov principle".. I just didn't came thare to figure out how to solve it with interfaces... I'm currently a litlle bit "frozen".. I imagine subclasses implementing various interfaces, that I could know in advance which set of interfaces I predicted for that purpouse, knowing that each interface guarantees me a certain methods, becaouse I imagine a kind of diversity among subclasses , so, for example, i wouldn't want to have all subclasses implementing , or having the same method.. Imight be just wrong and stubborn....

But, for a moment , I am very interested in "running inheritance backwards"...

So thank You very much Campbell Ritchie and Matthew Brown..
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
There's no extends used anywhere.

...And thank you, I just editted the mistake... thank you... : )))))
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Oh..

"And one more question"

How would You draw "running inheritance backwards" in UML?

: ))

[edit]
or I wil jump to the OO forum, after I work out a litlle bit on this, if I will stil have some questions....


do re mi...

I appritiate this so much.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Petar Tomičić wrote: . . . or I wil jump to the OO forum . . .
Probably best to stick to one thread.
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Sure... the Best. : )
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

Petar Tomičić wrote:How would You draw "running inheritance backwards" in UML?

I think I'd draw it as a <<dependency>> from the superclass to the subclass.
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Would that look somehting like in the picture which I drawed in Paint and attached it here?

And thank You very much for Your time...


[Thumbnail for dependency.jpg]

Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Is 'backward inheritance' or 'running backward inheritance' a terminology?! Or was it created just here...?


OCPJP 6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

Super classes should be oblivious of their sub classes. This is a really bad design.

If you want a selfInspect method, make it do nothing in class Q and override it in class Z:
Other sub classes of Q can now allow self inspection as well; they just need to override the method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Sure. Hi Rob Prime.
Not so often I need once in a while just to experiment. At this moment, for a day or few I'am only thinking and writing something, and experimenting something.. from dusk till over the dawn, literally... and I have.. just from the last few days the whole mathematical notebook filled with notes about observations..
I am doing extra effort . But really thank You for the program that You wrote to me, becaouse I can now compare two programms.., Yours which is a good design, and mine which came out of my intimate abstract proccess of detecting my thinking.
Sometimes for me personally one wrong example has a value of hundart good examples.
Thank You for telling me that this is a really bad design.

I am really proud on that small program that I wrote. There is something that I don-t know what is it that I am so dumb about, and this small program that I wrote touches that point where I'm so dumb, and it enlightes me a litlle bit, so it makes me a less dumber. I'm really proud on that program.

What reminds me on the epitaf of Paul Erdos> "Finnally i stoped to be more and more stupid" , but on Hungarian it sounds really much better. I know a litlle bit of Hungarian.

Also, I am native Croatian, speaking , reading, and thinking and writing in Croatian language all the time. Did you know that Nikola Tesla is a Croatian inventor, was borned and living in Croatia , just maybe two hours with a car from my place, and that a cravat (a forerunner of neck tie and bowtie) was invented by Croatians, and did You know that a mechanical pencil was invented by Croatian? Did You know that Croatia is between Italy and Hungary?



@Matthew Brown,
-Considering the UML question-

I figured out the question that I had:

If... there is... the situation like this:



can be written like this:



Then:

I think if I show it with the <<local>> association pointed to the Z class..
(and in the same time <<dependency>> , another association?)
How can I show that I am calling methodZ() on class Z only if it passes instanceof test (with [guards] )... with this.. Z z = (Z) this .... which is pseudo - "Z z = (Z)Z".... (running inheritance backwards)... and in the same time... existing instanceof test shows the possibility that there could be similar relationships like that more if there would be more subclasses?


Thank You people all for Your time, and thank You Rob Prime one more time. It was all very very valuable, all yours allways great advices. Now I am really tierd a litlle bit.

This was the question made like this, as it is, made on the best possible way for me to get some sense about something, before now, after I rest a bit, I will go to grasp 23 (is it 23) design patterns in one run, i just had to develop the way of thinking... And after that I will make three aplications, one of it is a web... three small applications... and with all of that i wish to be finished in two weeks, after which I have to run through Hibernate, .. and so many other things...

Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
I call it a Telescopic Eye.

It's Wonderfull.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
I always thought Croatia was between Serbia and Slovenia.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

Campbell Ritchie wrote:I always thought Croatia was between Serbia and Slovenia.

Depends which direction you're travelling.
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Look....
At my home no one calls Croatia Croatia, to everyone here it is Hrvatska. : )


[Thumbnail for Hrvatska.JPG]

Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
..and sorry, one detail, not to say it wrong, I writed that Nikola Tesla was Croatian, but actually he was Serbian borned and living in Croatia..
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
I must say that I am impresed that You both know for Croatia.

and this is very well said:

Depends which direction you're travelling.


: )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: running inheritance backwards