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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:56:21+00:00 2026-05-29T13:56:21+00:00

In Java it’s possible to write an abstract, super class with unimplemented, abstract methods

  • 0

In Java it’s possible to write an abstract, super class with unimplemented, abstract methods and non-abstract methods which invoke the abstract methods. Then in the subclass are the abstract methods implemented. When you then make an instance of the subclass, the super class uses the implementations in the subclass. How do I accomplish this in C++?

Here is what I mean, but in Java:

SuperClass.java

public abstract class SuperClass {

    public SuperClass() {
        method();
    }

    private void method() {
        unimplementedMethod();
    }

    protected abstract void unimplementedMethod();
}

SubClass.java

public class SubClass extends SuperClass {

    public SubClass() {
        super();
    }

    @Override
    protected void unimplementedMethod() {
        System.out.println("print");
    }

    public static void main(String[] args) {
        new SubClass();
    }
}

Would be awesome if you showed me how this is accomplished in C++. 🙂

  • 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-29T13:56:23+00:00Added an answer on May 29, 2026 at 1:56 pm

    In general, what you are looking for, is the virtual keyword. In a nutshell virtual declares the intent that this method can be overriden. Note that such a method can still have an implementation- virtual just makes it overrideable. To declare an “abstract method”, you can say declare intent of please provide an implementation in the derived class with = 0, as shown below. Such methods are called pure virtual in C++.

    However, there are some caveats that you should watch out for. As pointed out in a comment below, you were calling method() from within the SuperClass constructor. Unfortunately this is not possible in C++, due to the order in which objects are constructed.

    In C++ a derived class constructor immediately calls it’s superclass constructor before allocating its members or executing the body of the constructor. As such, the members of the base class are constructed first, and the derived class’ members are constructed last. Calling a virtual method from a base class will not work as you expect in Java, since the derived class has not been constructed yet, and thus the virtual methods have not been redirected to the derived implementations yet. Hope that makes sense.

    However, calling method() on a SuperClass object after creation will work as you expect: it would call the virtual function which would output “print”.

    class SuperClass {
    
    public:
        SuperClass() {
            // cannot call virtual functions from base constructor.
        }
        virtual ~SuperClass() { } // destructor. as Kerrek mentions,
                                   // classes that will be inherited from,
                                   // should always have virtual destructors.
                                   // This allows the destructors of derived classes
                                   // to be called when the base is destroyed.
    
    private:
        void method() {
            unimplementedMethod();
        }
    
    protected:
        virtual void unimplementedMethod() = 0; // makes method pure virtual,
                                                // to be implemented in subclass
    }
    

    SubClass.h

    class SubClass : public SuperClass {
    
    public:
        SubClass() : SuperClass() { // how the superclass constructor is called.
    
        }
    
        // no need for "override" keyword, if the methd has the same name, it will
        // automatically override that method from the superclass
    protected:
        void unimplementedMethod() { 
            std::cout << "print" << std::endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java Concurrency in Practice gives the following example of an unsafe class which due
java.util.concurrent API provides a class called as Lock , which would basically serialize the
Java Code In Java code I have class called IdentificationResult which has 3 members:
(Java question) If I reference a field in an inner class, does this cause
Java has the thread dump which is triggered by a signal 3 sent to
Java Code need to write an equivalent in objectiveC. CAn you please help me
java.sql.Connection is implemented into a verbose connection to get more information for unit tests.
java BufferReader has a method readLine which reads till '\n' or '\r' is read
Java code: import javax.swing.Timer; class Main { public static void main(String args[]) { MyListener
Java was claimed to be Once write, execute everywhere, but I wonder is it

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.