Change method argument type to a derived class in child class

how to access superclass variables in subclass in java
cast base class to derived class java
parent class reference child class object
call subclass method from superclass java
base class reference derived class object
can we pass an object of a subclass to a method expecting an object of the super class
how to call subclass method in java
java inheritance reference type

I am creating a DB interface and having two different database implementation to use this interface

For example:

class DBOptions {

class SQLliteDBOptions : public DBOptions { bool verySpecificSQLiteOption; };
class MySQLDBOptions : public DBOptions{ bool verySpecificMySQLOption; };

class DBIface {
  enum FileMode {
    READ = 1,
    WRITE = 2,
  virtual bool connect(char * filename, DBIface::FileMode mode, DBOptions * opt) = 0;
  virtual bool disconnect() = 0;

class SQLiteDB : public DBIface {
  bool connect(char * filename, DBIface::FileMode mode, SQLliteDBOptions * options)
  { std::cout << "connect form sqlite\n"; }
  bool disconnect() { std::cout << "disconnect from sqlite\n"; }

class MySQLDB : public DBIface {
  bool connect(char * filename, DBIface::FileMode mode, MySQLDBOptions * options)
  { std::cout << "connect form mysql\n"; }
  bool disconnect() { std::cout << "disconnect from mysql\n"; }

int main() {
  DBIface * sqlitedb = new SQLiteDB();
  SQLliteDBOptions * opt = new SQLliteDBOptions();
  sqlitedb->connect("file", DBIface::READ, opt);
  return 0;

The method connect in both derived classes are not considered an implementation for the pure method in the parent class. What should I do to resolve this problem?

I want my class to accept the proper DB option instance. I think I can dynamic cast the DB option to the type I need, but I guess this is not the best solution.

Should I create a setter method to set the DB options for each DB implementation?

I would say make the options parameter an argument to the derived database type's constructor. That way the derived type can require the correct options type without having to worry about matching the base signature. So long as the options parameter is on any virtual method then it has to be the same (or a base type via contravariance).

c++, I would say make the options parameter an argument to the derived database type's constructor. That way the derived type can require the  Type cast #1 is not safe because it assigns the address of a base-class object (Parent) to a derived class (Child) pointer. So, the code would expect the base-class object to have derived class properties such as gotoSchool() method, and that is false.

You have to declare a method in derived classes that has the same parameters as the pure virtual method connect(). That is, derived classes will be considered abstract classes too unless you override all the pure virtual functions.

I tried the solution proposed by SoronelHaetir. As expected it works, and I think it solves your problem in an elegant way.

class SQLiteDB : public DBIface{
    SQLiteDB(SQLliteDBOptions* opt) { pOtps = opt; }
    bool connect(char * filename, SQLiteDB::FileMode mode)
        std::cout << "connect form sqlite\n";
    bool disconnect() { std::cout << "disconnect from sqlite\n"; }

    SQLliteDBOptions * pOtps;

int main() {
    SQLliteDBOptions * opt = new SQLliteDBOptions();
    DBIface * sqlitedb = new SQLiteDB(opt);

    sqlitedb->connect("file", DBIface::READ);
    return 0;

And of course, virtual function in the abstract base class has to be modified accordingly:

virtual bool connect(char * filename, DBIface::FileMode mode) = 0;

"Converting" between base class and derived class types, A class A that is derived (inherits) from another class B inherits: All variables; All (​ordinary or non-constructor) methods  Multiple inheritance allows us to use the code from more than one parent class in a child class. If the same method is defined in multiple parent methods, the child class will use the method of the first parent declared in its tuple list.

I would get rid of connect and disconnect altogether. This functionality should be moved to constructors and destructors of derived classes, respectively. Each constructor may have different signature and that's perfectly OK since they are not virtual.

This way, if you have an object derived from DBIface, you can be absolutely sure it is connected.

This solution is compliant with the RAII idiom and therefore can be recommended as the preferred option.

Overriding methods by passing as argument the subclass object , The concept you initially refer to in your question is called covariant return types. Covariant return types work because a method is supposed to return an object  If you’re learning about classes in Windows PowerShell 5.0, one of the first new concepts that you’ll encounter is inheritance.When you create a class that is based on another class, your new subclass or child class automatically gets the inheritable members (e.g. properties and methods) of the parent class or base class.

Subclassing and Inheritance, A class in Java can be declared as a subclass of another class using the In this example, we shadow the instance variable sum to change its type from int to method signature (name and argument types) as a method in its superclass. of a method in the inheritance hierarchy of an object, the one in the “most derived”  Let’s have a quick understanding on this. If we derive a class from a base class and want to pass data from the derived class constructor to the constructor of the base class, a derived class constructor is required to call the constructor from its base class. We use base keyword for this type of initialization.

Why can't I pass a reference to a derived class to a function that takes, about passing a reference to the base class by reference. There is a double error CS1503: Argument '1': cannot convert from 'ref Derived' to 'ref Base' the type system. But the only time that S can be T or a subclass, “Why can’t I pass a reference to a derived class to a function that takes a reference to a base class by reference?” That’s a confusing question, but it’s phrased that way because the simpler phrasing is wrong! Ths misleading simplified phrasing of the question is “Why can’t I pass a reference to a derived class to a function that takes a base class by reference?”

All About Overriding in Java, You can change the method signature accordingly in your child class, i.e number of arguments, type and order of arguments, and return type. AbstractDog class or the Animal interface, then it must override all the inherited. Inheritance in C# and .NET. 07/05/2018; 26 minutes to read +10; In this article. This tutorial introduces you to inheritance in C#. Inheritance is a feature of object-oriented programming languages that allows you to define a base class that provides specific functionality (data and behavior) and to define derived classes that either inherit or override that functionality.

  • This is a classic problem. There's no universal good solution. One frequently seen solution is to make DBIface a template parameterised by dbOptions type. The other one is dynamic cast. A setter is worse than either one IMHO.
  • Yet another option is to remove connect from DBIface, leaving it in the child classes as independent methods. Since connect is not callable given just a DBIface reference anyway, there's no loss of functionality.
  • "it took me some time to find the problem" The problem was given and this is not a solution to the question.
  • @krisz you were totally right. I ended up "solving" a question that was not asked. Now, I just tried one solution proposed earlier to see how to implement it. Hope this is useful. Thanks