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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:08:24+00:00 2026-06-07T18:08:24+00:00

Consider the situation where I have an abstract class in Java; public abstract class

  • 0

Consider the situation where I have an abstract class in Java;

public abstract class Foo
{
    public abstract int myOperation();
}

Now, some of its subclasses may override myOperation like this;

class A extends Foo
{
    public int myOperation()
    {
            // Do stuff
    }
}

But if one subclass instead wants to return some other data type like;

class A extends Foo
{
    public Object myOperation()
    {
        // Do stuff
    }
}

I want the method name to be the same, to keep the design intact so that the clients don’t necessarily select which method to call. Is there a workaround for this other than having separate methods with one being an empty implementation or using Object as the return type? Or is this a seriously bad example of OO design?

I’ve heard about Covariant return types in C++ and wondering whether Java has some other mechanism for this.

I’m also free to use an interface here.

  • 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-07T18:08:25+00:00Added an answer on June 7, 2026 at 6:08 pm

    You can’t. Neither using inheritance, nor using an interface for this. It’s going to be a compiler error so you won’t be able to run your program at all.

    You could return java.lang.Object and return whatever object you want. In case you need to return a primitive, you could return its object wrapper.

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

Sidebar

Related Questions

Consider a situation. We have some specific C++ compiler, a specific set of compiler
Consider the following common situation: You have some MainView in your Cocoa application, loaded
Consider this situation where we have one product Laptop adapter and its stock as
So, please consider the following situation I have a super class of type Shapes
Consider situation when we have a class with couple of static methods that use
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
Consider the following situation: I have two android projects named P1 and P2 which
Consider the below situation I have a database table as below user_id user_rank ------------------
Consider a situation. I have an in-proc COM server that contains two COM classes.
Consider a situation we have two variables in SQL Server 2005's SP as below,

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.