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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:37:49+00:00 2026-05-25T14:37:49+00:00

In C++ and Java, or their respecting rules, what limits are placed on overiding

  • 0

In C++ and Java, or their respecting rules, what limits are placed on overiding abstract methods. Must you match the arguments or return type. I usually see abstract functions implemented with only a return type and no arguments, is it up to derived class to specify the rest. How does it work exactly?

  • 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-25T14:37:50+00:00Added an answer on May 25, 2026 at 2:37 pm

    Method overriding must have the same method signature of the parent method it’s overriding, otherwise it’s not called overriding.

    Java:

    public abstract class AbstractTest {
    
        public abstract void test() throws Exception;
    }
    
    public class ConcreteTest extends AbstractTest {
    
        @Override
        public void test() throws Exception {
    
        }
    }
    

    As you can see, ConcreteTest (which extends AbstractTest) must override test(). They have the same method name, return types and no method parameters. The subclass can omit the exceptions thrown from the base class and throw its own Exception. The subclass can also add additional (un)checked exception.

    As Peter Lawrey mentioned, a java interface methods are implicitly abstract method (See my SO question on Java Abstract Interface).

    What is crucial here is that the method visibility cannot change in this case (as it’s a hierarchical visibility, i.e. private->protected->public). This is valid though:

    public abstract class AbstractTest {
    
        protected abstract void test() throws Exception;
    }
    
    public class ConcreteTest extends AbstractTest {
    
        @Override
        public void test() throws Exception {
    
        }
    }
    

    (The parent has a protected method and the subclass can override the same method and only has 2 choice for visibility: protected or public).

    Also, Suppose you have

    public class B {
    
    }
    
    public class D extends B {
    
    }
    
    public abstract class Base {
    
        public abstract B foo();
    }
    
    public class Derived extends Base {
    
        @Override
        public D foo() {
            // TODO Auto-generated method stub
            return new D();
        }
    
    }
    

    You will see that Derived returns a D and not a B. Why is that? That’s because the derived class follows the same signature as the parent class and the return type of the derived class is a subtype of the return type of the parent class.

    So, I can have this:

    Base pureBase = new Derived();
    B b = pureBase.foo(); //which returns class D
    
    if (b instanceof D) {
       //sure, it is, do some other logic
    }
    

    In C++, you can get similar effect, using Covariant Return types

    C++

    class AbstractTest {
    public:
        virtual void test() = 0;
    };
    
    
    class ConcreteTest : AbstractTest {
    public:
        void test() {
            //Implementation here...
        }
    };
    

    In C++, a class with a pure virtual function (a virtual function that ends with a =0) is known as an Abstract class. The subclass (in C++, class extension is delimited by :) override the pure virtual method (except it doesn’t contain the =0). It has the same signature has its parent class.

    Going back to our Java example, suppose you have:

    class B {
    
    };
    
    class D : B {
    
    };
    
    class Base {
    public:
        virtual B* foo() = 0;
    }
    
    class Derived : Base {
    public:
        D* foo() {
            return new D();
        }
    }
    

    The same reasoning (as explained in java) is done here. Covariant return types also works with protected and private inheritance. More on Covariant return types.

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

Sidebar

Related Questions

Mixing the use of primitive data types and their respective wrapper classes, in Java,
Stateless beans in Java do not keep their state between two calls from the
Are there any Java VMs which can save their state to a file and
Many Java applications that use shell scripts to configure their environment use the JAVA_HOME
Some programming languages such as Java and C# include encryption packages in their standard
C++ has std::vector and Java has ArrayList , and many other languages have their
I'm a front-end web developer at a company using Java on their server. As
I want to enable the users of my website to upload their Java programs
I've seen some interesting claims on SO re Java hashmaps and their O(1) lookup
Should interfaces in Java reside in their own directory? Or should both the interface

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.