Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:09:13+00:00 2026-05-30T00:09:13+00:00

I get compilation error when I do this cast: RandomAccessFile raf = new RandomAccessFile(…)

  • 0

I get compilation error when I do this cast:

RandomAccessFile raf = new RandomAccessFile(...)
InputStream is = (InputStream)raf;

RandomAccessFile is supposed to subclass InputStream although not directly.

From docs:

RandomAccessFile implements DataInput which inturn DataInputstream & InputStream

Why is this invalid?

Also appreciate your input on what would be the right way to use the RandomAccessFile as InputStream?

I am thinking of wrapper approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:09:15+00:00Added an answer on May 30, 2026 at 12:09 am

    RandomAccessFile extends Object, and does not extend InputStream.

    If you want get an InputStream from a RandomAccessFile I think implementing a wrapper class is your simplest bet. Luckily the only abstract method of InputStream is read().

    RandomAccessFile implements DataInput which inturn DataInputstream & InputStream

    DataInputStream is a subclass of InputStream, which also happens to implement DataInput. The inheritance and interface implementation tree looks like this:

               InputStream      DataInput
                   \              /   \
                    \            /     \
                     \          /       \
                    DataInputStream   RandomAccessFile
    

    You could use a DataInputStream anywhere where you could use an InputStream or a DataInput. You could use a RandomAccessFile anywhere where you could use a DataInput.

    But you can’t go up and then down in the inheritance hierarchy like this using casts. In particular, casting a class to a subclass (or an interface to an implementation) will raise a ClassCastException unless the object happens to be an instance of the child class.

    Even though two classes happen to extend Object that doesn’t mean they are interchangeable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Eclipse always gives me this compilation error when I try to get the length
You get a compilation error if you define the string parameter to have a
I get the following compilation error: error: expected `;' before 'it' Here's my code:
I get the following compilation error with the following source code: Compilation Error: Type
I get the following compilation error fatal error C1189: #error : ERROR: Use of
I get this error when I try to import ssrc.spread : ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/ssrc/_spread.so, 2):
It says (note this is during run-time I get this error)... I have no
When assigning a method to a Func -type, I get the compilation error Type
I get compilation error when i try to compile the following..... Please comment. Here
I've a class implemented as below. In the constructor, I get a compilation error.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.