fred2
0
Q:

c# inheritance constructor

class parent_class
{
  
}
class child_class : parent_class
{
//Will simply move Data and Methods from the parent_class to the child class.
}
2
public otherclass{

    public otherclass(a,b){
          a=0;
          b=1;
    }

}

public baseClass : otherclass{
  
  	public baseClass(string str,int a, int b): base(a,b){
    	str="whatever";
    }
  
}
1
// a Pseudo-example using interfaces. <--- Worked great for me!

public interface IPrinter
{
   void Send();
   string Name { get; }
}

public class PrinterType1 : IPrinter
{
  public void Send() { /* send logic here */ }
  public string Name { get { return "PrinterType1"; } }
}

public class PrinterType2 : IPrinter
{
  public void Send() { /* send logic here */ }
  public string Name { get { return "Printertype2"; } }

  public string IP { get { return "10.1.1.1"; } }
}


// ...
// then to use it
var printers = new List<IPrinter>();

printers.Add(new PrinterType1());
printers.Add(new PrinterType2());

foreach(var p in printers)
{
  p.Send();

  var p2 = p as PrinterType2;

  if(p2 != null) // it's a PrinterType2... cast succeeded
    Console.WriteLine(p2.IP);
}
0

// ----------------- INHERITANCE and POLYMORPHISM ------------------ //


// ----- TOP CLASS ----- //
class Parent
{
  protected int ID;   // This will be inherited by the child class
  
  public Parent()   // This constructor will automatically be called when we create a child object 
  {
    ID = 0;
  }
  
  public Parent(int Id)   // This constructor will automatically be called when we create a child object 
  {
    ID = Id;
  }
  
  
  public virtual void Method1 (string someInput)   // The "virtual" keyword allows you to override this method
  {
    Console.WriteLine("Hi there, this method will be inherited");
    Console.WriteLine(someInput);
  }
  
  protected void Method2 ()
  {
    Console.WriteLine("Hi there, this method will also be inherited");
  }
  
    protected void Method3 ()
  {
    Console.WriteLine("Hi there, this method will also be inherited");
  }
  
}


// ----- LOWER CLASS ----- //
class Child : Parent
{
	pritave int count;    // This class has both the "count" and "ID" properties, since the "ID" was inherited
    
    
	public Parent()   // Both the parent and child base constructors are called  
    {
      count = 0;
    }
    
    
    public Parent(int Id) : base (Id)  // Both the parent and child second constructors are called  
    {
      count = 0;
    }
    
    
    public override void Method1 (string someInput)  // This will override the original Method1 funtion
    {	
    	base.Method1 (someInput);  // This will call the original method from the parent that now, also belongs to the child 
        // ... some code ...
    }
    
        
    protected new void Method2 ()   // This will not override but will instead make it a priority over the other Method2() 
    {                               // This is only used if you create an object like: Parent obj = new Child() and not if you create: Child obj = new Child()  
      Console.WriteLine("Make it do something different");
    }
    
    
    public sealed override void Method3 ()   // This "sealed" keyword will stop other subclasses that derive from the child, from overriding this method again 
    {                              
      Console.WriteLine("Make it do something different");
    }
    
    
	public void Method4 (string someInput, int count)
    {	
    	base.Method1 (someInput);  //Or just: Method1 (someInput) since, in this case, the methods are different
        this.count = count;
    }

}
 
-1
// C# program to illustrate the 
// concept of inheritance in the 
// constructor when the derived 
// class contains a constructor 
using System; 
  
// Class Tank to give the 
// dimension of the tank 
class Tank { 
  
    double t_radius; 
    double t_height; 
  
    // Properties for Radius and Height 
    public double Radius 
    { 
        get {  
               return t_radius;  
            } 
  
        set { 
               t_radius = value < 0 ? -value : value; 
            } 
    } 
  
    public double Height 
    { 
        get {  
               return t_height;  
            } 
  
        set {  
              t_height = value < 0 ? -value : value;  
            } 
    } 
  
    // Display the dimension of tanks 
    public void DisplayDimension() 
    { 
        Console.WriteLine("The radius of tank is :" + Radius  
                 + " and the height of tank is :" + Height); 
    } 
} 
  
// A derived class AreaOfTank  
// inheriting Tank Class 
class AreaOfTank : Tank { 
  
    string Color; 
  
    // Constructor 
    public AreaOfTank(string c, double r, double h) 
    { 
  
        // from base class 
        Radius = r; 
        Height = h; 
  
        // from derived class 
        Color = c; 
    } 
  
    // Return area of tank 
    public double Area() 
    { 
        return 2 * 3.14 * Radius * Height; 
    } 
  
    // Display the color of tank 
    public void DisplayColor() 
    { 
        Console.WriteLine("The Color of tank is " 
                                        + Color); 
    } 
} 
  
// Driver Class 
class GFG { 
  
    // Main Method 
    static void Main() 
    { 
  
        // Create and initialize the 
        // object of AreaOfTank 
        AreaOfTank t1 = new AreaOfTank("Green", 6.0, 12.0); 
        t1.DisplayColor(); 
        t1.DisplayDimension(); 
        Console.WriteLine("Area is " + t1.Area()); 
    } 
}
-1

New to Communities?

Join the community