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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:06:06+00:00 2026-05-27T04:06:06+00:00

Consider the following code : public class Main implements Vehicle, Car { public static

  • 0

Consider the following code :

public class Main implements Vehicle, Car {
  public static void main(String[] args) {
    Main generalConcreteClass = new Main();
    System.out.println(((Vehicle) generalConcreteClass).TYPE); //**Line 1**

    Vehicle vehicle = new Main();  //**Line 2**
    System.out.println(vehicle.TYPE);//  Here there is no Ambiguity since vehicle is of TYPE vehicle

    System.out.println(((Car)vehicle).TYPE);  // **Line3** This doesn't throw ClassCastException..
  }
}

Here, both the interfaces Vehicle and Car have the same constant TYPE with different value.

There will be ambiguity with generalConcreteClass at Line1, so type-cast is necessary and any one of TYPE can be accessed.

Line2 : vehicle object has reference of Vehicle interface.

Line3 : How can I cast my vehicle object to Car type and still access the constant TYPE of Car. How is becomes visible to vehicle object. Or how does it work internally??

Now, if I dont make my class implement Car interface, then a similar type-casting of vehicle object to type Car throws a ClassCastException.

  • 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-27T04:06:06+00:00Added an answer on May 27, 2026 at 4:06 am

    Static methods and fields are not polymorphic. You should never use an instance to access a static field. Use Vehicle.TYPE or Car.TYPE.

    If you want to access the type of an object polymorphically, then use a getType() instance method.

    Line 3 doesn’t throw a ClassCastException because the object’s concrete type is Main, and a Main is a Car, so casting works.

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

Sidebar

Related Questions

Consider the following code:- class Name { {System.out.println(hi);} public static void main(String[] args) {
Consider the following code: namespace ConsoleApplication1 { class Program { static void Main(string[] args)
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
Consider the following C# code: class Program { static public void Print(string toPrint) {
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code: public class ReadingTest { public void readAndPrint(String usingEncoding) throws Exception
Please consider the following code: public class Person ( public string FirstName {get; set;}
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {

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.