Call constructor from base class - 2 level above - oop

In WinDev, I have a base class, let say BaseClass. This base class has a constructor:
PROCEDURE CONSTRUCTOR(param1, param2, param3)
I have a first child class that extends the base class, let say childClass1. This first child has a constructor with the only 2 firsts arguments:
ChildClass1 is a class inherits from BaseClass
...
PROCEDURE CONSTRUCTOR(param1, param2)
Constructor BaseClass(param1, param2, 1)
And I would like to have a class that inherits from ChildClass1 but uses the constructor from BaseClass. It seems impossible without redefinning the 3 parameters constructor of BaseClass inside the ChildClass1.
Here is what I tried to do:
ChildClass2 is a class inherits from ChildClass1
...
PROCEDURE CONSTRUCTOR(param1, param2)
Constructor BaseClass(param1, param2, 2)
But is says that BaseClass is neither a base class or a member of ChildClass1.
Does the only solution is to redefine the constructor of BaseClass inside ChildClass1 so that ChildClass2 can use it?
This seems as a lack of OOP handling.
Cheers,

The explicit constructors of the base class or member must be called in first statement of the constructor of the derived class.
Example:
//----Declare the BaseClass1 class
BaseClass1 is Class
BaseClass1Member is int
END
//---- Constructor of BaseClass1
PROCEDURE Constructor(Param1)
Info("Constructor of BaseClass1 => " + Param1)
//----Declare the class named BaseClass2
BaseClass2 is Class
BaseClass2Member is int
END
//---- Constructor of BaseClass2
PROCEDURE Constructor(Param1)
Info("Constructor of BaseClass2 => " + Param1)
//---- Declaration of DerivedClass
DerivedClass is Class
// Inheritance of BaseClass1 whose
// Constructor expects a parameter
inherits from ClassBase1
// BaseClass2 member whose
// Constructor expects a parameter
DerivedClassMember is BaseClass2
END
//----Constructor of DerivedClass
PROCEDURE Constructor()
// Explicit call to Constructor
Constructor BaseClass1(10)
Constructor DerivedClassMember(20)

Related

Access the instance by which methods of an interface are delegated inside of the class

Is there a way to access the instance by which methods of an interface are delegated inside of the class?
class Class1(): Interface2 by Class2() { // NOTE: Class2() is here a concrete implementation by which the methods of Interface2 are delegated.
// I want to access the instance by which the Interface2 is delegated (Class2()) in here.
}
Fore now I do it like this:
private val class2Instance = Class2()
class Class1(): Interface2 by class2Instance { // NOTE: Class2() is here a concrete implementation by which the methods of Interface2 are delegated.
val class2: Class2 by ::class2Instance // the value class2 now grants me access to class2Instance
}
But I don't think this is a good way, because the class has to access a value that is declared outside any class.
You can make Class2 a property and constructor parameter of Class1 and then delegate by that parameter:
class Class1(val class2: Class2): Interface2 by class2
That way you do not need the reference to the class2Interface defined outside the Class1 from inside Class1, but you have to pass it as constructor parameter:
val class1 = Class1(class2Instance)

How to access a variable in child class from an obj in parent class object in systemverilog

So I want to modify a variable which is not directly in parent class but is in a class which is instantiated in parent class. for eg.
class cfg;
int a
endclass
class parent;
cfg cfg1
endclass
class child extends parent;
<how to change variable "a" here which is declared in cfg?>
endclass
Your first problem is using the terms parent and child with respect to inheritance. Those terms imply two separate objects. What you have is a base class type and an derived class type. When constructing a derived class object, you have access to everything in the base class as if it were all in the same class.
class cfg;
int a
endclass
class base;
cfg cfg1
endclass
class derived extends base;
//<how to change variable "a" here which is declared in cfg?>
//Answer: Just use cfg1.a
endclass

Why base classes must have default new?

I got this error
https://learn.microsoft.com/en-us/dotnet/visual-basic/misc/bc30387
And I wonder why.
What about if I never want to call parameterless new in both base and derived classes?
I can do that if I don't use inheritance. Why using inheritance means I can no longer do so?
To repeat the issue
What's the explanation? So NOT every classes need parameterless new but classes with inheritance must? That doesn't make sense.
What about if I never call derived class with parameter less constructor. I don't intent for the class to ever be constructed without parameter.
For example, say, I want to create a class without parameterless constructor. I can do that right.
But say I want to split the class into two. Parent and child class. I want BOTH not to ever have parameterless constructor.
It seems that I can't do that can I? If such is the case, can anyone please confirm it.
The link says that if you have an explicit constructor in your base class with parameters and no parameterless one than you cannot leave your derived class without constructor. Because VB.NET cannot create an implicit constructor for derived class.
If you don't write any constructors for both, it is perfectly valid.
public class Base
End Class
Public Class Derived
Inherits Base
End Class
However you cannot declare a derived class without an explicit constructor like below. Because VB.NET cannot determine how to initialize base class.
public class Base
Public sub New(ByVal Item As Integer)
End Sub
End Class
Public Class Derived
Inherits Base
End Class
To overcome this issue you can declare a default constructor on derived class which calls base class constructor with a default value.
public class Base
Public sub New(ByVal Item As Integer)
End Sub
End Class
Public Class Derived
Inherits Base
Public Sub New()
MyBase.New(5)
End Sub
End Class

Instance of derived class calling empty method in base

I have a base class and derived class, the base class has two methods which are overridden in the derived class. Let's take the following scenario:
My base class
public class BaseClass
public Overridable function method1()
method2()
End Function
public Overridable function method2()
' Empty !!!
End Function
End class
My Derived class
public class DerivedClass
public Overrides function method1()
MyBase.method1()
End Function
public Overrides function method2()
' Some code !!
End Function
End class
Now I have created an instance of the derived class and call method1().
Dim obj as new DerivedClass()
obj.method1()
method1() in the derived calls method1() in the base, which calls method2 in the base. the strange thing to me that method2 in base, which is empty, called the method2 in the derived! ..
Can any one explain to me what is happening and how calling methods with inheritance done? and what concept was applied with this hierarchy ?
method1() in the derived calls method1() in the base, which calls method2 in the base.
This is where you are wrong.
Since method2 is overridable, it is a "virtual method". Thus, all calls to method2 call the overridden version. This includes the method2 invocation in method1 of the base class:
Public Class BaseClass
Public Overridable function method1()
method2() ' <-- If method2 has been overridden, the overridden
' version is called here.
End Function
...
End Class
Overridable methods in VB, like virtual methods in C#, are not called directly but typically employ what is called a vtable which contains pointers to methods. In this way when you have an instance of type BaseClass the vtable will point to the BaseClass version of the overridable method. When you have an instance of DerivedClass a pointer to its version of the method overwrites what was in the vtable and so when the method is invoked it will call the derived class version. For more information on vtables see this question

Does an inherited class automatically implement an Interface from its base class?

Suppose I have piece of code like this:
Public Interface ISomething
....
End Interface
Public Class SomeClass
Implements ISomething
....
End Class
Now, if I inherit from SomeClass like this:
Public Class InheritedClass
Inherits SomeClass
....
End Class
will InheritedClass automatically implements ISomething, or must I use Implements ISomething in the InheritedClass' definition?
The interface was already implemented by the base class. Your derived class will thus implement it as well since it inherits the base class implementation. If you want to alter the base class implementation then you should declare the implementation method(s) virtual so you can override them.
Yes, the interface will be inherited as well.