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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:30:14+00:00 2026-06-18T20:30:14+00:00

class One { void show1() { System.out.println(super class); } } class Two extends One

  • 0
class One
{
    void show1()
    {
       System.out.println("super class");
    }

}

class Two extends One
{
    static void show2()
    {
       System.out.println("subclass");
    }


}
public class Cast 
{
    public static void main(String[] args)

    {
      One o=(One) new Two();
      o.show1();

    }

}

How does this statement work in this code One o=(One) new Two(); ? Why can’t I use like this
Two o=(One) new Two(); ? I am confused of using casting, though o here is super class variable.. Why can’t I refer sub class method using o?

  • 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-18T20:30:15+00:00Added an answer on June 18, 2026 at 8:30 pm

    How this statement work in this code One o=(One) new Two();

    It means that an object of class Two is created and pointed by referenc of class One. Type casting is not required.

    why can’t i use like this Two o=(One) new Two();

    That is polymorphism. derived class can’t point to base class.

    Rule of thumb: Base class can point to any derived class. (not vice-versa). So, you can cast a derived class to base class and not other way round.

    though o here is super class variable. why can’t i refer sub class method using o.

    Right, but the actual object is of derived class and the reference is of base class. Only those methods of derived class will be accessible which are overriden in derived class.

    Try following lines of code to see yourself:

    Two obj = new One(); // Error
    One obj = new Two(); // OK
    Two obj = (Two) new One(); // Error
    

    See this: ClassCastException, ClassCastException, and Explanation of "ClassCastException" in Java

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

Sidebar

Related Questions

I have the following two classes, one inherits from the other Class A{ void
I've a snippet class T{ int y; public static void main(String... s){ int x;
For example. class One(models.Model): text=models.CharField(max_length=100) class Two(models.Model): test = models.Integer() many = models.ManyToManyField(One, blank=True)
Can we able to create two instance of log4j in class one to write
I have one class. Class First { private Second second; public First(int num, String
friends, i am using following code inside asyncTask public class AsycLoaderFromDbAndMapInjector extends AsyncTask {
So I have two classes. One is a class called Main. The Main class
According to the documentation: public static <T> int binarySearch(T[] a, T key, Comparator<? super
I constructed a class with one String field. Then I created two objects and
I have a class: class One def initialize; end end I need to create

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.