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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:29:00+00:00 2026-05-27T22:29:00+00:00

Coming from a Java perspective I was surprised to find that you can only

  • 0

Coming from a Java perspective I was surprised to find that you can only override base methods that have the virtual keyword.
In Java you use the final keyword to declare that a method can’t be overridden.

I had the idea in my head that you only rarely want to prohibit overriding so that someone can extend your class how they see fit.

So in C++ if you feel that someone might want to at some stage inherit from your class (maybe years later someone thinks its a cool idea) do you make all your methods virtual?

Or is there some critical reason for wanting to prohibit this in C++ that I am unaware of?

for Reference this was the experimenting i did in each language:

Java

    public class Base {

    void doSomething(){
    System.out.println("Doing the base thing");
    }
    }
    public class Derived extends Base {

    void doSomething(){
    System.out.println("Doing the derived thing");
    }

    public static void main(String... argv){
        Base object = new Derived();
        object.doSomething();
    }
    }

C++

    class Base
    {
    public:
        Base(void);
        ~Base(void);
        virtual void doSomething();
    };

    void Base::doSomething(){
        std::cout << "I'm doing the base thing\n" << std::endl;
    }

    class Derived :
        public Base
    {
    public:
        Derived(void);
        ~Derived(void);
        void doSomething();
    };

    void Derived::doSomething(){
        std::cout << "I'm doing the dervied thing" << std::endl;
    }


    int main(void){

        Base * object = new Derived;

        object->doSomething();
        return 0;

    }
  • 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-27T22:29:01+00:00Added an answer on May 27, 2026 at 10:29 pm

    duffymo and Als are steering you in the right direction. I just wanted to comment on one thing you said:

    So in C++ if you feel that someone might want to at some stage inherit
    from your class (maybe years later someone thinks its a cool idea) do
    you make all your methods virtual?

    From a software engineering perspective: if you don’t have an immediate use for inheritance and aren’t using interfaces, then I don’t recommend declaring your methods virtual.

    Virtual methods come with an ever so slight performance degradation. For non-critical code paths the perf impact is likely negligible. But for class methods that get invoked a lot, it can add up. The compiler can’t do as much inlining and direct linkage. Instead, the virtual method to be invoked has to be looked up in the v-table array at runtime.

    When someone on my coding team starts off a design conversation with “someone might want at some point later…” that’s when my “future proofing” anti-pattern alarm goes off. Designing for extensibility is one thing, but “features for the future” should be put off until then.

    And besides – the guy years later who thinks it’s a cool idea – Let him be the one to own converting the class methods to be virtual. You’ll be onto bigger projects by then anyway. 🙂

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

Sidebar

Related Questions

I'm learning C++ (coming from java) and recently discovered that you can pass functions
I'm coming from a place without macros (Java/Python/C#/Scala) so perhaps my perspective is distorted
Coming from Java, I expected to find some other GUI layout in Visual C++
I am new to the MVC/Linq party (coming from Java). I know that I
Coming from a Java background, I like it when I was warned that I
Coming from java/groovy/grails we are currently moving to rails. We have been having an
I am coming from Java, and want to know if I can 'set' an
I am learning ActionScript 3.0. Coming from Java world I can easily relate to
I'm new to Python, coming from Java and C. How can I increment a
Coming from Java, I am learning Scala. I am interested in game and virtual

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.