top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to implement inheritance using VB.NET/C#?

+4 votes
438 views
How to implement inheritance using VB.NET/C#?
posted Nov 19, 2013 by Deepankar Dubey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

When we set out to implement a class using inheritance, we must first start with an existing class from which we will derive our new subclass. This existing class, or base class, may be part of the .NET system class library framework, it may be part of some other application or .NET assembly, or we may create it as part of our existing application. Once we have a base class, we can then implement one or more subclasses based on that base class. Each of our subclasses will automatically have all of the methods, properties, and events of that base class ? including the implementation behind each method, property, and event. Our subclass can add new methods, properties, and events of its own - extending the original interface with new functionality. Additionally, a subclass can replace the methods and properties of the base class with its own new implementation - effectively overriding the original behavior and replacing it with new behaviors. Essentially inheritance is a way of merging functionality from an existing class into our new subclass. Inheritance also defines rules for how these methods, properties, and events can be merged.

answer Mar 13, 2014 by Manish Tiwari
0 votes

Inheritance: BaseClass.cs

using System;

public class ParentClass
{
    public ParentClass()
    {
        Console.WriteLine("Parent Constructor.");
    }

    public void print()
    {
        Console.WriteLine("I'm a Parent Class.");
    }
}

public class ChildClass : ParentClass
{
    public ChildClass()
    {
        Console.WriteLine("Child Constructor.");
    }

    public static void Main()
    {
        ChildClass child = new ChildClass();

        child.print();
    }
}

Output:

Parent Constructor.
Child Constructor.
I'm a Parent Class.

Derived Class Communicating with Base Class: BaseTalk.cs

using System;

public class Parent
{
    string parentString;
    public Parent()
    {
        Console.WriteLine("Parent Constructor.");
    }
    public Parent(string myString)
    {
        parentString = myString;
        Console.WriteLine(parentString);
    }
    public void print()
    {
        Console.WriteLine("I'm a Parent Class.");
    }
}

public class Child : Parent
{
    public Child() : base("From Derived")
    {
        Console.WriteLine("Child Constructor.");
    }
    public new void print()
    {
        base.print();
        Console.WriteLine("I'm a Child Class.");
    }
    public static void Main()
    {
        Child child = new Child();
        child.print();
        ((Parent)child).print();
    }
}

Output:

From Derived
Child Constructor.
I'm a Parent Class.
I'm a Child Class.
I'm a Parent Class.
answer Nov 20, 2014 by Manikandan J
Similar Questions
+2 votes

Is there any VB.NET equivalent to the C# var keyword?

+3 votes

By-default all the methods & variables in class are private.

In C#.net Main() method is private then how compiler access this Main() Method out side the class because compiler is not part of our program then how compiler access this Main() method?

Here we loose our data abstraction property ? Can anyone put some light on it?

+3 votes

I want get Number of indexed pages of any website so, it will give me exact number of pages in google.
What I have tried:

Uri queryUri = new Uri(string.Format("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:{0}", url));
using (WebClient client = new WebClient())
{
    string getString = client.DownloadString(queryUri);
    dynamic s = JsonConvert.DeserializeObject(getString);
    var IndexedPage = s["responseData"]["cursor"]["estimatedResultCount"].Value;
    lblIndexedPageRecv.Text = IndexedPage;
}

its working but not giving me right answer

my code is giving different values then other online webCheckers like one of this: http://www.pingler.com/seo-tools/tools/google-indexed-pages-checker

...