Can we overload an abstract method of an abstract class?
-
I have 3 classes overriding a function of base abstract class A. The child classes are 1, 2 & 3. Child classes override function "public abstract void hello(var1, var2)".
Following is what I want to implement:
abstract class A
{
public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
}** Currently the above code throws error and wants me to implement the new inherited abstract function in class 1 & 2.
-
I have 3 classes overriding a function of base abstract class A. The child classes are 1, 2 & 3. Child classes override function "public abstract void hello(var1, var2)".
Following is what I want to implement:
abstract class A
{
public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
}** Currently the above code throws error and wants me to implement the new inherited abstract function in class 1 & 2.
No, an
abstract
method in class A must be implemented by all (non-abstract) derived classes. If the new method is only available in class 3, then add it to class 3. You can't add it to class A, because it's not valid for every instance of a class derived from class A.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
No, an
abstract
method in class A must be implemented by all (non-abstract) derived classes. If the new method is only available in class 3, then add it to class 3. You can't add it to class A, because it's not valid for every instance of a class derived from class A.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks Richard.
-
I have 3 classes overriding a function of base abstract class A. The child classes are 1, 2 & 3. Child classes override function "public abstract void hello(var1, var2)".
Following is what I want to implement:
abstract class A
{
public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
}** Currently the above code throws error and wants me to implement the new inherited abstract function in class 1 & 2.
You implementation force Class 1 and 2 to implement "hello(var1, var2,var3)" just because you want to have a extra method for Class 3. This is not a good design to follow. You should follow Open to extend and close to modification methodology. you might create an extension of class A and implement it for Class 3. Please refer the approach below:
abstract class A
{
public abstract void hello(var1, var2); // This will be only for class 1 & class 2 (Existing function)
}
abstract class A1 : A
{
public abstract void hello(var1, var2, var3); // Want to make this available for class 3 only. (Want to add this new function)
}Implement Class A for Class 1 and Class 2 Implement Class A1 for Class 3
Maddy