arman_aegit
6
Q:

c# inheritance

class parent_class
{
  
}
class child_class : parent_class
{
//Will simply move Data and Methods from the parent_class to the child class.
}
2
// Parent Class
public class A
{
    public void Method1()
    {
        // Method implementation.
    }
}
// inherit class A in class B , which makes B the child class of A
public class B : A
{ }

public class Example
{
    public static void Main()
    {
        B b = new B();
        // It will call the parent class method 
        b.Method1();
    }
}
1
// declaring an interface 
public interface A { 
      
    // method of interface 
    void mymethod1(); 
    void mymethod2(); 
} 
  
// The methods of interface A  
// is inherited into interface B 
public interface B : A { 
      
    // method of interface B 
    void mymethod3(); 
} 
0
//example

class derived: super
{private int arg;
// constructor
public derived(int arg, int super_arg): base(super_arg)
{
this.arg=arg;
this.super_arg=super_arg;
}
}
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