Base constructor in base class instead of derived class in C# - c#

I'm learning how to properly apply OO principles in C#. I came across a little problem and I can't figure out how to solve it. I run into the following problem:
The current situation:
public abstract class Foo
{
protected Foo()
{
//does nothing
}
}
public class Bar : Foo
{
public BarX ( int a, int b, int c) : base()
{
this.a = a;
this.b = b;
this.c = c;
}
public doStuff()
{
//does stuff
}
}
public class BarY : Foo
{
public Bar( int a, int b, int c) : base()
{
this.a = a;
this.b = b;
this.c = c;
}
public doStuff()
{
//does stuff
}
}
The point is that I hafe different types of Foo. In this case it would be circles and rectangles. I want them to have the same constructors as each type has the same attributes. They only have a different doStuff() method. I have tried many combinations but every time I try to move the arguments to the base class's constructor it tells me that 'some class does not contain a constructor that takes 0 arguments' (or 3 arguments) depending how I move around my code.
My question is how I can move the assignment of a, b and c's values to the abstract class's constructor?

The following will work:
public abstract class Foo
{
protected Foo(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public abstract void doStuff();
}
public class Bar : Foo
{
public Bar(int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}
public class BarY : Foo
{
public BarY(int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}

That's because BarY and BarX are calling a default constructor (base()) which doesn't exist in your base class when you only have a parameterised constructor. You need to pass the arguments through as well (base(a, b, c)):
public abstract class Foo
{
protected Foo(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public abstract void doStuff();
}
public class Bar : Foo
{
public BarX (int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}
public class BarY : Foo
{
public Bar(int a, int b, int c) : base(a, b, c)
{
}
public override void doStuff()
{
//does stuff
}
}

Related

Two projects, a derived class should be able to access one method but not the other one

I was asked this question in interview, but couldn't answer it.
There are two projects: P1 and P2
P1 has a class A with methods M1 and M2
P2 has a class B and class C, both of these classes (inherit or reference)* from class A in Project P1
Class B should be able to access method M1 but not M2
Class C should be able to access method M2 but not M1
*I don't remember if he said inherit or said reference
How can this be done?
You want to use interfaces to extract methods from class.
P1:
public interface IM1
{
void M1();
}
public interface IM2
{
void M2();
}
// Not public
internal class A : IM1, IM2
{
public void M1() {}
public void M2() {}
}
P2:
public class B
{
private readonly IM1 _m1;
}
public class C
{
private readonly IM2 _m2;
}
You can implement some like: https://dotnetfiddle.net/0mYxdU
public interface InterfaceCommon
{
void Function();
}
public class Implementacion1 : InterfaceCommon
{
public void Function()
{
Console.WriteLine("I am Method1");
}
}
public class Implementacion2 : InterfaceCommon
{
public void Function()
{
Console.WriteLine("I am Method2");
}
}
public abstract class A
{
private readonly InterfaceCommon interfaceCommon;
protected A(InterfaceCommon interfaceCommon)
{
this.interfaceCommon = interfaceCommon;
}
public void CallFunction()
{
interfaceCommon.Function();
}
}
public class B : A
{
public B() : base(new Implementacion1()) { }
}
public class C : A
{
public C() : base(new Implementacion2()) { }
}
public class Program
{
public static void Main(string[] args)
{
var b = new B();
b.CallFunction();
var c = new C();
c.CallFunction();
Console.ReadLine();
}
}

how to impliment constructor in inheritence in c#

I am very beginner in c#. I created base class and derived class but i do not understand behavior of constructor in derived class it gives error "does not contain constructor that take 0 arguments" how to use it in derived class
class A
{
public int x, y, z;
public A(int i, int j)
{
x = i;
y = j;
}
public void add(int i,int j)
{
z=x + y;
Console.WriteLine(z);
}
}
class B : A
{
public B (int k, int l)
{
x=k;
y=l;
}
public void multi(int k,int l)
{
z = x * y;
Console.WriteLine(z);
}
}
Usage:
class Program
{
static void Main(string[] args)
{
A ad = new A(5,6);
B m = new B(8, 9);
}
}
Since B inherits A, it has to include a call to A's constructor, called the base contructor.
class B : A
{
public B (int k, int l)
: base(k, l)
{
}
}
This calls the code in A's constructor, populating x and y with the values in k and l.

How to Access method in Parent from Child Class

I have one method which is in child class and I want to fetch that in Parent with the help of this.
public class Class1
{
private class Class2
{
public void Add(int a, int b) // Method in Class 2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class 1
{
// Get this Add method by This.Add ??
// Not able to fetch the Add method here.
}
}
you have declared the method but it is in class2. that means you need to create an instance of class2 in order to use the method
public class Class1
{
private class Class2
{
public void Add(int a, int b) // Method in Class 2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class 1
{
class2 cs = new class2();
cs.Add(4,5);
}
}
You would either have to create an instance of Class2 in the constructor of Class1 and use an instance method, or change the method Add to static in Class2
Static version
Something like
public class Class1
{
private class Class2
{
public static void Add(int a, int b)
{
}
}
public Class1()
{
Class2.Add(1,2);
}
}
Instance version
Something like
public class Class1
{
private class Class2
{
public void Add(int a, int b)
{
}
}
public Class1()
{
new Class2().Add(1,2);
}
}
Maybe have a look at static (C# Reference)
public class Class1
{
private class Class2
{
public void Add(int a, int b) // Method in Class 2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class 1
{
Class2 newclass2 = new Class2();
newclass2.Add(1, 2);
// Get this Add method by This.Add ??
// Not able to fetch the Add method here.
}
}
You need to create instance for Class2
public class Class1
{
private class Class2
{
public Class2() // constructor of Class2
{
}
public void Add(int a, int b) // Method in Class2
{
this.Add(a, b);
}
}
public Class1() // constructor of Class1
{
Class2 cs2 = new Class2();
cs2.Add(4,5);
}
}

How to override a method inherited from a base class, which in turn implemented it from an interface?

I have a class B that inherits from class A, which in turn inherits from interface I. This interface exposes a method M which, of course, is implemented by A, but I would like to override it in B. Moreover, I would like to call A.M from B.M. How do I do that?
EDIT: The answers made me feel kind of stupid, especially since I know what virtual means and, in fact, I have tried it:
class A
{
virtual void I.M() // fails
I never even considered not implementing the interface explicitly.
Thank you all.
Well, either you need to make the method virtual in A, or you need to reimplement the interface in B, which gets messy. Here's the simpler version:
using System;
public interface IFoo
{
void M();
}
public class A : IFoo
{
public virtual void M()
{
Console.WriteLine("A.M");
}
}
public class B : A
{
public override void M()
{
base.M();
Console.WriteLine("B.M");
}
}
class Test
{
static void Main()
{
IFoo foo = new B();
foo.M();
}
}
... and here's the version which reimplements IFoo, hiding A.M() instead of overriding it:
using System;
public interface IFoo
{
void M();
}
public class A : IFoo
{
public void M()
{
Console.WriteLine("A.M");
}
}
public class B : A, IFoo
{
public new void M()
{
base.M();
Console.WriteLine("B.M");
}
}
class Test
{
static void Main()
{
IFoo foo = new B();
foo.M();
}
}
Note that if you then had:
A a = (A) foo;
a.M();
it would only call A.M(), not B.M().
You can use the New keyword to hide the base M as well, heres a short linqpad program
The diffrence between virtual and new
http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-s-the-difference-between-code-override-code-and-code-new-code.aspx
void Main()
{
var x = new B();
x.M();
}
public interface I
{
void M();
}
public class A : I
{
public void M()
{
"A.M".Dump();
}
}
public class B : A
{
public new void M()
{
"B.M".Dump();
base.M();
}
}
Results:
B.M
A.M
Make M virtual in A
interface ISome
{
void M();
}
class B : ISome
{
public virtual M()
{
}
}
class A : B
{
public void override M()
{
base.M();
}
}
When you implement method M in class A, implement it as "virtual", then when you want to override it in class B use the "override" keyword, which will allow you to do just that.
interface I {
void M();
}
class A : I {
virtual void M() {
}
}
class B : A {
override void M() {
//Do stuff;
base.M();
}
}

What is the correct syntax for calling an overloaded method using 'this()' in C#?

I may have this wrong, but I've seen the way of creating an overloaded method that calls itself in the definition. It's something like:
public void myFunction(int a, int b)
{
//Some code here
}
public void myFunction(int a) : this (a, 10)
{ }
This is not the correct syntax, I know, but I can't find the correct syntax anywhere for some reason. What is the correct syntax for this?
You are confusing constructor syntax for method syntax. The only way to do that for a method is the obvious:
public void myFunction(int a, int b)
{
//Some code here
}
public void myFunction(int a)
{
myFunction(a, 10) ;
}
although as of C#4, you can use a optional parameters:
public void myFunction(int a, int b = 10)
{
//Some code here
}
What you wrote is close to right for constructors:
public class myClass
{
public myClass(int a, int b)
{
//Some code here
}
public myClass(int a) : this (a, 10)
{ }
}
Just do this:
public void myFunction(int a, int b)
{
//Some code here
}
public void myFunction(int a)
{
myFunction(a, 10)
}
You're confusing overloading with overriding. You can call a base constructor from an derived class like this:
public MyConstructor(int foo)
:base (10)
{}
public class BaseClass {
public virtual void SomeFunction(int a, int b) {}
}
public class Derived : BaseClass {
public override SomeFunction(int a, int b) {
base.SomeFunction(a, b * 10);
}
}

Categories