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 7441083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:57:21+00:00 2026-05-29T10:57:21+00:00

Very simple code for test: interface Base { void interfaceTest(); static final String m

  • 0

Very simple code for test:

interface Base {
    void interfaceTest();
    static final String m = "1";
}

interface Child extends Base {
    void interfaceTestChild();
}

class BaseClass implements Base {
    @Override
    public void interfaceTest() {
        System.out.println("BaseClassInterfaceTest");
    }
}

class ChildClass implements Child {

    @Override
    public void interfaceTest() {
        System.out.println("ChildClassInterfaceTest");
    }

    @Override
    public void interfaceTestChild() {
        System.out.println("interfaceTestChild");

    }
}

public class Src {
    public Child testFunc() {
        Base x = new BaseClass();
        return (Child)x;      <==Here got an "ClassCastException"
    }

    public static void main(String args[]) {
        Src testSrcInstance = new Src();
        testSrcInstance.testFunc().interfaceTest();
    }
}

In the line return (Child)x; I got a “ClassCastException” and I feel very confused about it, for Child extends Base, so x should be converted to Child successfully. this kind of conversation is imitated by some android codes:

the getText() method of EditText is:

public Editable getText() {
        return (Editable) super.getText();
    }

and the super class of EditText is TextView, of which the getText() method is:

public CharSequence getText() {
    return mText;
}

mText is a CharSequence, and note that Editable extends CharSequence, so you can see, these android codes cast CharSequence to Editable, just as me, cast Base to Child, any difference?

  • 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-29T10:57:21+00:00Added an answer on May 29, 2026 at 10:57 am

    in the line return (Child)x; i got a “ClassCastException”, i feel very confused about it, for Child extends Base, so x should be converted to Child successfully.

    No, it will only work if x *actually refers to an instance of some type which implements Child. In this case, it doesn’t – it only refers to an instance of BaseClass. That doesn’t specify any behaviour for interfaceTestChild(), so what would expect to happen if you’d been able to call it?

    Base x = new BaseClass();
    // Imagine this had worked...
    Child child = (Child)x;
    // What would this do? There's no implementation!
    child.interfaceTestChild();
    

    Java only lets you cast to a type which the value actually supports – i.e. some type in the inheritance hierarchy of the object that the value refers to.

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

Sidebar

Related Questions

I have a very simple code: package mygame; public class RunGame { public static
This very simple code gives me tons of errors: #include <iostream> #include <string> int
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
I'm stuck! I have this very simple test code and I can't get it
I wrote a very simple test code of printf uint64_t: #include <inttypes.h> #include <stdio.h>
I have this very simple test code: s=helloCustomer is_contain_customer = s=='helloCustomer' || s.include? 'Customer'
Very simply put, I have the following code snippet: FILE* test = fopen(C:\\core.u, w);
I have some very simple code to generate an assembly and invoke a method
I have some VERY simple code to return the title for a section header:
I'm coding a simple code editor for a very simple scripting language we use

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.