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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:06:55+00:00 2026-05-25T11:06:55+00:00

Overriding Methods in java have the following features : 1> The Overrinding method should

  • 0

Overriding Methods in java have the following features :

1> The Overrinding method should have the same argument list as the Parent class method.

2> The return type should be the same/subclass of the return type for the parent class method.

3> The access level should be same or less restrictive as the parent class method.

4> The overriden method can throw the same or Narrower exception and not a broader one.

*Just wanted to know why is it so for point *

*2 – why subclass why not a superclass?*

3 – why the access level should be less restrictive?

4 – why it should throw narrow exception?

As per my understanding its just that if i am creating a parent class refrence to create a child class object and trying to run down each of the scenario then

Lets assume A is parent class and B is child class both having method printAndReturnSomething()

public class A{

       public B printAndReturnSomething(){
                S.O.P("Inside A Print");
                return new B();
              }
}

Now we have the child class B as

public class B extends A{
       public A printAndReturnSomething(){ // I know this isn't possible to return A but had it been then
                S.O.P("Inside A Print");
                return new A();
              }
}

Now if i do something like this

A a =new B();

and now as i have a reference of A so i expect the return type to be of type B

B returnedValue=a.printAndReturnSomething(); // But it actually calls the child class method and hence returns A. So here comes the contradiction.

Similaraly for the scenarios 3 and 4. Is my understanding correct? Am i missing something else more relevant?

  • 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-25T11:06:56+00:00Added an answer on May 25, 2026 at 11:06 am

    All of this can be summarized by saying that “a subclass has to behave as if it was a superclass”. So if Derived extends Base and I have an object x of type Derived, then I want it to behave exactly as if it were of type Base.

    So if x.foo() returns some type T, and Base::foo() returns a type S, then I want to be able to treat x.foo() as an S, so T better be the same as or a subclass of S.

    Similarly, x.foo() should only throw exceptions that Base:foo() promises. It can’t start throwing new, unexpected exceptions.

    If Base::foo() is public, then so should x.foo() be. You can’t suddenly have a tighter restriction on the derived class, because the base class promised me that it was public.

    Always think of inheritance as “behaves like the super class”, or “can be treated like the superclass”, and all this should be clear.

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

Sidebar

Related Questions

Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class
I'm creating a class named TetraQueue that inherits System.Collections.Generic.Queue class overriding the Dequeue method,
I am just refreshing the oops features of the java. So, I have a
I Have following in my struts.xml file <action name=ProductVerification class=com.frontend.ProductVerification> <result name=success>/jsp/product_verification.jsp</result> <result name=input>/jsp/product_verification.jsp</result>
I'm new to Java, and I've read over some tutorials on overriding methods, but
I have a problem with overriding the equals method in an Enum to make
I'd like to re-implement a method of a Java class. For example, for hi.length()
I have a class that is something like the following: public class Foo {
in java im used to extending enum values or overriding methods like this :
Let's have the following class hierarchy: public class MyType { } public class MySuperclass<T

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.