• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Need Clarification/Explanation

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy everyone!
My name is Martin as you can see to your left, and I'm new to JavaRanch. I've been spying in on some topics, and finally have a question of my own, so I thought I'd give it a shot )
I have seen numerous declarations such as this...

I guess my brain just isn't processing how you can have an object reference, "in", be an InputStream, but give it a new FileInputStream at the same time?
My only thought is that FIS is a subclass of IS, but it still just isn't clicking to me (and until it does, it's going to bug the heck out of me, and confuse me).
If at all possible, could someone explain the theory behind such a declaration as above? Thanks, and I hope to help others in the future as I learn more! )
Martin
 
Ranch Hand
Posts: 42
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FileInputStream indeed extends InputStream
When you are trying to cast an object to a superclass, you do not need to explicitly write the cast, the cast is implicit.
i.e. upcasting is implicit and downcasting has to be implicit.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Martin!
And welcome to Polymorphism. You may want to take a look at some of The Campfire Stories for an easy introduction to the subject (see "How my Dog learned Polymorphism").
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The analogy is quite common if you think.
Well, there are is-a(-type-of) relationships in objects just like there are with other common day to day life.
For example, since a dog has all the characteristics of a living thing, it is a type of a living thing. And ofcourse, a human also has all the characteristics of living things so a human is also a living thing, but there are subtle differences between Humans and Dogs( when viewed from a larger frame of reference ) which proves that dogs are not a type of humans and vice-versa.
In OOPS, when there is a class/object of a type of something else, then you can have a valid statement :
something xx = new resemblingThing();
or
Livingthing pup = new Dog();!!
Regds
Lupo
To push it further, when interfaces are implemented, the same is-a kind of relation is formed.
 
Martin Clifford
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you would use this type of declaration if you want the object to have all of the functionality of both the sub and superclasses?
From the Campfire Stories example:

The reference 'd' would have all of the overridden methods and members of Dog(), but also anything original to Animal?
So if Dog hadn't overridden the method eatFood(), then it would take on any generic form of the method from it's parent class, right?
Thank you all for your help! I'm starting to get it, I think )
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you would use this type of declaration if you want the object to have all of the functionality of both the sub and superclasses?
Not exactly. You would use this type of declaration if you wanted to use a reference with the interface declared in the parent class and the behavior (partly or fully) implemented in the subclass.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reference 'd' would have all of the overridden methods and members of Dog(), but also anything original to Animal?
The reference would have the interface as defined in the super class. Of course, it would actually refer to an object that could do and have all of the things that a Dog could do and has.
So if Dog hadn't overridden the method eatFood(), then it would take on any generic form of the method from it's parent class, right?
Right.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clarifying Dick's example a bit further, your reference to an instance of Dog via a reference to Animal would have access to all of the methods of Animal, but with the implementation of Dog. So, Animals eat. Dogs eat with a whole lot of slobber. Your d would eat with a whole lot of slobber.
Note that any method that isn't available on Animal isn't available on d, even if that method is available for Dog. So, for example, Dogs bark. But Animals don't. So

wouldn't work (wouldn't compile, even).
but

would. Notice the explicit downcast to Dog - if the cast couldn't be completed [for instance, if you tried to cast to Plant, which wasn't in the Animal hierarchy], you'd get a ClassCastException.
[ December 03, 2002: Message edited by: Tina Coleman ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice explanation, but the name's Dirk.
 
Tina Coleman
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mea culpa. The difficulties of responding to messages in a window that doesn't display the tree of messages.
[Could I beg off and say that there was another poster in the tree somewhere named Dick who had given an excellent explanation? Nah. Probably just have to 'fess up that I goofed.]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you respond, below your input window is another window that displays the thread that you are responding to.
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic