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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:22:28+00:00 2026-06-05T09:22:28+00:00

I have a subclass named child which inherits a class named parent. I am

  • 0

I have a subclass named child which inherits a class named parent. I am trying to override the method in the super class. I have experimented a little. I have created the overriden method with its return type short which is the subtype of int. As far I know I can do so. And the method is legally overriden. whenever I call the method with the reference of the super class, the compiler generates an incompatible return type error. What’s the problem here ? My code is given below:

class parent
{

    int test()
    {
        System.out.println("called inside parent\n");
        return 1;
    }
}

class child extends parent
{

    short test()
    {
        System.out.println("called inside child\n");
        return 1;
    }
}

class Myclass
{

    public static void main(String[] args)
    {
        parent a=new child();
        a.test();
    }
}
  • 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-05T09:22:30+00:00Added an answer on June 5, 2026 at 9:22 am

    short which is the subtype of int

    Wrong, it is not. The value range of short is a subset of that of int, but this is a totally different issue. A short value can indeed be converted to an int (even implicitly), still a short is not an int.

    Primitive types are not regular classes, so these can’t have supertype/subtype relationships. Therefore you can’t use contravariance of return type in this case: once you declared a return type of int in the base method, all of its overrides must return int.

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

Sidebar

Related Questions

I have created a class named ListCell which is a subclass of UITableViewCell .
If I have a parent-child that defines some method .foo() like this: class Parent
Lets say I have a class named Dog which is a subclass of Animal
Suppose I have a base class named Visitor, and it has 2 subclass Subscriber
In my project, say I am instantiating a class named SubClass , which extends
If you declare an inheritance hierarchy where both the parent and child class have
I have created a UIView subclass in order to implement a custom drawRect method.
I have created a class named Foo : @interface Foo:NSObject{ int myInt; } @property
In my Cocoa Application I have created a Custom View Class (subclass of NSView),
I have subclass that inherits from a dict. On __getitem__ method I'd like to

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.