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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:00:47+00:00 2026-06-04T18:00:47+00:00

Out of mere curiosity, is it possible to assign a variable to two types?

  • 0

Out of mere curiosity, is it possible to assign a variable to two types?

Since I don’t have my technical terms straight in this matter, let me clarify a bit. Normally you would have a static and dynamic type:

Animal a = new Dog();

Where the dynamic type has to inherit the static type:

public class Dog extends Animal {...}

But is it possible to ensure that the dynamic type extends/implements multiple types?

So that the Dog has to extend the Animal-class and implement an interface?

I’d imagine something like:

Animal [implements Happy] a = new Dog();

I’m quite sure it’s just crazy talk, but is it possible? And if not, what’s the closest alternative?

I’m thinking this only would be relevant in cases of ensuring a variable is of an interface you made yourself and a class that you cannot edit.

One way I can think of is determining it runtime by casting it every time you use it:

try {
    @SuppressWarnings("unused")
    Animal a = (Happy) new Dog();
} catch (ClassCastException e) {
    //Is not of type Happy
}

//Using a method from the Happy-interface
try {
    ((Happy)a).smile();
} catch (ClassCastException e) {
    //Is not of type Happy
}
  • 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-04T18:00:49+00:00Added an answer on June 4, 2026 at 6:00 pm

    No, it is not possible directly. You can think of some workarounds like:

    abstract class HappyAnimal extends Animal implements Happy
    

    And then use that type. Of course your classes must extend HappyAnimal, it won’t work if they extend Animal and implement Happy directly.

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

Sidebar

Related Questions

out of curiousity and because I don't have my copy of the standard at
Out of interest, is it possible to determine the size of a JavaScript variable,
Out of curiosity is it possible to open a form based on row selection
out of curiosity is there a way to access this.color from the paint function?
Out of curiosity/excitement I have been reading whatever information I can find about ARC
Out of curiosity im interesting in finding out which ranges are reserved for localhost
Out of curiosity, what is the reasoning behind typeof not being a regular method
Out of curiosity, why are sometimes multiple Java .class files generated for a class
Out of curiosity, I wonder what can people do with parsers, how they are
Out of curiosity I've been playing with jQuery to determine the browser's screen size,

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.