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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:12:24+00:00 2026-06-17T13:12:24+00:00

public class InheritanceDemo { public static void main(String[] args) { ParentClass p = new

  • 0
public class InheritanceDemo {

    public static void main(String[] args) {

        ParentClass p = new ParentClass();
        ChildClass c = new ChildClass();

        //Casting ChildClass to ParentClass
        ParentClass pc = new ChildClass();
        pc.parentClassMethod(); //Output: Parent Class Method (as expected)

        //Again Casting Parent Class to ChildClass explictly
        //Question 1 for this code
        ChildClass cp = (ChildClass) pc;
        cp.parentClassMethod(); //Output: Parent Class Method (unexpected)

        ChildClass cc1 = (ChildClass) new ParentClass();
        cc1.parentClassMethod(); //Compiles, but Run Time Error

        ChildClass cc2 = (ChildClass) p;
        cc2.parentClassMethod(); //Compiles, but Run Time Error

    }
}

class ParentClass {

    public void parentClassMethod(){
        System.out.println("Parent Class Method");
    }

}

class ChildClass extends ParentClass {

    public void ParentClassMethod(){
        System.out.println("Parent Class Method From Child Class");
    }

    public void ChildClassMethod(){
        System.out.println("Child Class Method");
    }

}

Question1:

Now, I have a method called parentClassMethod in both ParentClass and ChildClass classes(Overridden). When I cast the ParentClass to ChildClass and then call the parentClassMethod, why is it executing the ParentClass method instead of the method from ChildClass if cp is refering to ChildClass?

Question2:

(i) ChildClass cp = (ChildClass) pc;

(ii) ChildClass cc1 = (ChildClass) new ParentClass();
(iii) ChildClass cc2 = (ChildClass) p;

If (i) is working fine, why not (ii) or (iii)?

Because I am casting from ParentClass to ChildClass in both the cases?

  • 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-17T13:12:25+00:00Added an answer on June 17, 2026 at 1:12 pm

    Now, I have a method called parentClassMethod in both ParentClass and ChildClass classes(Overridden).

    No you don’t. You have a method named parentClassMethod in ParentClass and a method named ParentClassMethod in ChildClass. Since all Java identifiers are case-sensitive, there is no association between the two. ParentClassMethod does not override parentClassMethod from ParentClass.

    If (i) is working fine, why not (ii) or (iii)?

    In (ii) and (iii) you are trying to cast an instance of ParentClass to an instance of ChildClass. That is not allowed, as a ChildClass is-not-a ParentClass, any more than an Object is a String.

    In (i) you are trying to cast an instance of ChildClass (stored in a reference declared as ParentClass) to a ChildClass, which is allowed.

    When casting, it’s the runtime type that counts (in other words, what T is used in new T()).

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

Sidebar

Related Questions

public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Reverse { public static void main(String[] args) { char name[]=prakash.toCharArray();////initialisng array ////////swapping
public class SomeClass{ public static void main (String[] args){ if(true) int a = 0;//
public class Test { public static void main(String[] args) { final String test1 =
public class Guess { public static void main(String[] args){ <sometype> x = <somevalue>; System.out.println(x
public class readBin { public static void main(String[] args) throws IOException{ long time1 =
public class Db_try1 { public static void main(String[] args) { String driver=oracle.jdbc.OracleDriver; String url=jdbc:oracle:thin:@localhost:1521:xe;
public class Main { public static void main(String[] args) throws InterruptedException { ClassA a
public class newsidlink{ public static void main (String[] args) throws Exception{ String msg=ABCD NEWSBULLETIN01
public class Test { public static void main(String[] args) { } } class Outer

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.