How can I make a method abstract in a child class?
-
public abstract class Base { public int GetInt() { return 1; } } public abstract class AbstractChild : Base { public abstract int GetInt(); } public class Child : AbstractChild { public override int GetInt() { return 2; } }
I want all implementers of AbstractChild to implement GetInt method. So, I decide to make the method abstract even though it's not originally abstract in the Base class. Then I implement it in the Child class. But when I execute the following code, it surprisingly returns 1:
Base b = new Child();
Console.Out.WriteLine(b.GetInt());// prints 1Since b is of type Child, I would normally expect the overridden method in Child class to be called. Why is the method in base class being called? Am I misunderstanding something?
-
public abstract class Base { public int GetInt() { return 1; } } public abstract class AbstractChild : Base { public abstract int GetInt(); } public class Child : AbstractChild { public override int GetInt() { return 2; } }
I want all implementers of AbstractChild to implement GetInt method. So, I decide to make the method abstract even though it's not originally abstract in the Base class. Then I implement it in the Child class. But when I execute the following code, it surprisingly returns 1:
Base b = new Child();
Console.Out.WriteLine(b.GetInt());// prints 1Since b is of type Child, I would normally expect the overridden method in Child class to be called. Why is the method in base class being called? Am I misunderstanding something?
Why do you need to make
GetInt
abstract inAbstractChild
? Why not make it abstract inBase
?Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
public abstract class Base { public int GetInt() { return 1; } } public abstract class AbstractChild : Base { public abstract int GetInt(); } public class Child : AbstractChild { public override int GetInt() { return 2; } }
I want all implementers of AbstractChild to implement GetInt method. So, I decide to make the method abstract even though it's not originally abstract in the Base class. Then I implement it in the Child class. But when I execute the following code, it surprisingly returns 1:
Base b = new Child();
Console.Out.WriteLine(b.GetInt());// prints 1Since b is of type Child, I would normally expect the overridden method in Child class to be called. Why is the method in base class being called? Am I misunderstanding something?
Hi, If you view the compiler warnings you should see what's wrong.
'AbstractChild.Child.GetInt()' hides inherited member
'AbstractChild.AbstractChild.GetInt()'.
To make the current member override that implementation,
add the override keyword. Otherwise add the new keyword.Alan.
-
Hi, If you view the compiler warnings you should see what's wrong.
'AbstractChild.Child.GetInt()' hides inherited member
'AbstractChild.AbstractChild.GetInt()'.
To make the current member override that implementation,
add the override keyword. Otherwise add the new keyword.Alan.
Hi Adding new keyword does not solve the problem. It still returns 1
-
Hi Adding new keyword does not solve the problem. It still returns 1
Hi, An abstract method must be implemented using the override modifier.
public abstract class Base {
public int GetInt() {
return 1;
}
}public abstract class AbstractChild : Base {
// new: completely replace the base class method
// abstract: inheritors must implement the method
public new abstract int GetInt();
}public class Child : AbstractChild {
// override: implement an abstract method
public override int GetInt() {
return 2;
}
}When first encountered these concepts are not easy but if you review the information on modifiers in the c# reference and then write some code to test your understanding you'll soon be there. Alan.
-
Hi, An abstract method must be implemented using the override modifier.
public abstract class Base {
public int GetInt() {
return 1;
}
}public abstract class AbstractChild : Base {
// new: completely replace the base class method
// abstract: inheritors must implement the method
public new abstract int GetInt();
}public class Child : AbstractChild {
// override: implement an abstract method
public override int GetInt() {
return 2;
}
}When first encountered these concepts are not easy but if you review the information on modifiers in the c# reference and then write some code to test your understanding you'll soon be there. Alan.
Do you know why the following code is returning 1 while I have completely replaced the base class method with
new
operator as you suggested?Base b = new Child();
Console.Out.WriteLine(b.GetInt());// prints 1 -
Do you know why the following code is returning 1 while I have completely replaced the base class method with
new
operator as you suggested?Base b = new Child();
Console.Out.WriteLine(b.GetInt());// prints 1Hi, When you redefine a method with the new modifier it is no longer known to the parent class. The confusing part is that the original method still exists in Base and this is called when you assign the AbstractChild instance into a Base type variable. If the code is altered (again) we can achieve what I think you originally wanted. 1) declare public virtual int GetInt() in Base to ensure that the method can be overridden. 2) public override abstract int GetInt() in AbstractChild This overrides the base class method with an abstract method 3) public override int GetInt() in Child as required by the abstract modifier in AbstractChild. Putting all of that together we have
public abstract class Base {
// virtual: method can be overridden
public virtual int GetInt() {
return 1;
}
}public abstract class AbstractChild : Base {
// override the base class method
// abstract: inheritors must implement this method
public override abstract int GetInt();
}public class Child : AbstractChild {
// override: implement an abstract method
public override int GetInt() {
return 2;
}
}class Program {
static void Main(string[] args) {
Base c = new Child();
Console.WriteLine(c.GetInt());
Console.ReadLine();
}
}The use of the override modifier throughout ensures that the most derived method is always accessible. Hope you are following the logic, we are both learning here! Alan.
-
Hi, When you redefine a method with the new modifier it is no longer known to the parent class. The confusing part is that the original method still exists in Base and this is called when you assign the AbstractChild instance into a Base type variable. If the code is altered (again) we can achieve what I think you originally wanted. 1) declare public virtual int GetInt() in Base to ensure that the method can be overridden. 2) public override abstract int GetInt() in AbstractChild This overrides the base class method with an abstract method 3) public override int GetInt() in Child as required by the abstract modifier in AbstractChild. Putting all of that together we have
public abstract class Base {
// virtual: method can be overridden
public virtual int GetInt() {
return 1;
}
}public abstract class AbstractChild : Base {
// override the base class method
// abstract: inheritors must implement this method
public override abstract int GetInt();
}public class Child : AbstractChild {
// override: implement an abstract method
public override int GetInt() {
return 2;
}
}class Program {
static void Main(string[] args) {
Base c = new Child();
Console.WriteLine(c.GetInt());
Console.ReadLine();
}
}The use of the override modifier throughout ensures that the most derived method is always accessible. Hope you are following the logic, we are both learning here! Alan.
Thank you very much for your response Alan! It's working :) I didn't know that a method with override modifier can have no body. But with help of override abstract, it can. great to know. The original code I posted was translated from a java code from Head First Design Patterns book (chapter 3, Decorator Pattern). Do you know java? It seems that the original code I posted will return 2 written in Java.
-
Thank you very much for your response Alan! It's working :) I didn't know that a method with override modifier can have no body. But with help of override abstract, it can. great to know. The original code I posted was translated from a java code from Head First Design Patterns book (chapter 3, Decorator Pattern). Do you know java? It seems that the original code I posted will return 2 written in Java.
Maysam Mahfouzi wrote:
I didn't know that a method with override modifier can have no body.
Actually I didn't either and was a bit surprised too. I'm not very familiar with java, it's one of my read-only languages, but in contrast to c# it probably has a more relaxed syntax. c# tends to make very few assumptions and as a consequence requires that intentions are stated explicitly. Alan.