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

  • SEARCH
  • Home
  • 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 8662457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:44:27+00:00 2026-06-12T16:44:27+00:00

Consider the following classes in Java: interface I{ } class A implements I{ }

  • 0

Consider the following classes in Java:

interface I{
}
class A implements I{
}

class B extends A {
}

class C extends B{
}
And the following declarations:
A a = new A();
B b = new B();

Once you have a = (B)(I) b; the code will compile and run. And don’t understand why at all I need to cast into Interface and class B. It would work, in my opinion, when a = b; fine. Can some one explain me the logic of the above explicit casting what make the code run fine.

But once you have I i = (C) a; it will fail at run-time because ‘a’ does not point to an object of class C. Why does ‘a’ needs to point to an object of class C? Besides that I don’t get the logic of casting into C class. Anyway you will have reference of I interface. Best regards

  • 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-06-12T16:44:28+00:00Added an answer on June 12, 2026 at 4:44 pm

    Casting an object to a given class or interface is only possible if the casted object indeed extends this class or implements this interface. If it does not (i.e. if the object is not an instance of the the class or interface), you’ll get a ClassCastException. Notev that casting an object to a class doesn’t change the type of the object. It only allows referencing it as another type, that it also is, extends or implements.

    Let’s see your examples:

    a = (B)(I) b;
    

    This first casts the object b, which is of type B. to the interface I. This is OK since B extends A, and A implements I, which means that B also implements I.

    Then it casts the same object a second time to the class B. b is an instance of B, so this works.

    I i = (C) a;
    

    This casts the object a, which is of type A, to the class C. A doesn’t extend C, so this is impossible.

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

Sidebar

Related Questions

Consider the following Java exception classes: public class BarException extends RuntimeException { // [...]
Consider the following code : public class LIMSGrid extends ClientEventSource implements Focusable, FramingBlockWrapper {
Consider the following classes in Java class A { protected void methodA() { System.out.println(methodA()
Consider the following example. I have an interface MyInterface, and then two abstract classes
Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>
Consider I have the following classes: class User { [Key] public Guid Id {get;
Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
Consider that I have the two following nested classes: public class Foo { public
Consider I have the following classes as ViewModels public class Address { public String
Consider the following data classes: class TopLevel { public TopLevel() { collection = new

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.