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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:01:12+00:00 2026-05-29T05:01:12+00:00

I have 3 classes A,B and c as follows A.java class A { protected

  • 0

I have 3 classes A,B and c as follows

A.java

class A
{
    protected A(){
        System.out.println("A");
    }
    void show()
    {
        System.out.println("showA");
    }
}

B.java

class B extends A
{
    B(){
        System.out.println("B");
    }
    void show()
    {
        System.out.println("showB");
    }
}

C.java

class C extends B
{
    C(){
        System.out.println("C");
    }
    void show()
    {
        System.out.println("showC");
    }
    public static void main(String... args)
    {
        A a= (B)new C();
        a.show();
    }
}

When executed gives the output

D:\java\rmi\Hello>javac C.java

D:\java\rmi\Hello>java C
A
B
C
showC

I know a superclass cannot be casted to a subclass.But in the output why is it executing the C class method (show) when there is a cast to the super class B?

A a= (B)new C();

And if this is right then what is it that is getting casted to B?
I mean here new C() would call the C constructor and hence the respective outputs but
what is the difference between new C().show(); and (B)new C().show(); what is getting casted here?

  • 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-29T05:01:16+00:00Added an answer on May 29, 2026 at 5:01 am

    Class B does not extend C, so it has no knowledge of it and connot be casted to C. With java, you can down cast but not upcast.

    OK:

    A a= (B)new C();
    

    -> C inherits from B, so cast is possible

    Not OK

    A a= (C)new B();
    

    -> B does not inherit from C, so it cannot be cast to it

    see also: Downcasting in Java

    Edit:
    Please consider to edit your question, as most users tend to correct an error first. (Remove the error and break it down to your original question)

    “what is the difference between new C().show(); and (B)new C().show();”
    This is called ‘polymorphism’. there is no difference between the two calls, as java will always execute the method of the lowest level of the hierarchy.

    For example:

    class Bird{
     public void fly(){
       System.out.println("I am flying");
    }}
    
    class Duck extends Bird{
     public void fly(){
       System.out.println("I can not fly");
    }}
    
    class Test{
     public static void main(String[] args){
      Bird[] birds = {new Bird(), new Duck()};
      for (Bird b: birds){
       b.fly();
      }
    }
    

    This would output:

    I am flying
    I cannot fly
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Java classes: B, which extends another class A, as follows :
I have two Java-Classes as follows: public class MyClass { ... } public class
I have two classes Address and Employee as follows: public class Address { public
New to FluentNHibernate =D I have a parent/children classes as follows: public class Parent
I have seen some C++ classes with a destructor defined as follows: class someClass
I have a Java class which is located in src/java that references classes from
I have a Parent.java class and 4 child classes as Child1.java , Child2.java and
In my code I have three classes as follows: Forum , Forum::Thread and Forum::Post
I'm new to Java and have the following question regarding inner classes: When implementing
I have a hello.clj as follows. (ns hello) (defn hi [] (println HI)) Normally,

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.