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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:59:38+00:00 2026-06-18T04:59:38+00:00

Consider following Java classes: public class Animal { public static void printText(Object obj) {

  • 0

Consider following Java classes:

public class Animal {
    public static void printText(Object obj) {
        System.out.println(obj.toString());
    }
}

class Tiger extends Animal {
    // Contains some unimportant methods.
}

And now, when the following is typed into the main method, the compiler won’t give any errors even though that cast will result in an error. Why?

public static void main(String[] args) {
    Animal animal = new Animal();
    ((Tiger)animal).printText(animal);  // <= ?? no error in the compiler ??
}
  • 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-18T04:59:39+00:00Added an answer on June 18, 2026 at 4:59 am

    In your example, it might be easy to know that the cast is wrong – but in the general case it is not.

    What if you had a method Animal getRandomAnimal() that might return Tiger as well. (Or the more common case, a method Animal getAnimal() that might be overriden by subclasses to return a Tiger).

    At compile time, you could not possibly know if

    Tiger tiger = (Tiger)getRandomAnimal();
    

    is valid or not, this is known only at run time.

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

Sidebar

Related Questions

Consider the following classes in Java class A { protected void methodA() { System.out.println(methodA()
Consider the following Java class: public class Foo { public static void doStuff() {
Consider the following two Java classes: a.) class Test { void foo(Object foobar) {
Consider the following Java method: public static void f(int n) { if (n<=1) {
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
Let's consider the following code in Java. package obj; final class First { public
Consider the following Java exception classes: public class BarException extends RuntimeException { // [...]
Consider the following program: import java.util.List; import java.util.ArrayList; public class TypeTest { public static
Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>
Consider the following method in Java: public static boolean expensiveComputation() { for (int i

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.