Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to call method in derived class with base class instance.
Following code giving me compile time error.
C#
class Parent
    {
        public void Display()
        {

        }
    }

    class Child : Parent
    {
        public void Display1()
        {

        }
    }

    class MyDemo
    {
        public static void Main()
        {
            Child c1 = new Child();
            c1.Display();

            Parent p1 = new Parent();
            p1.Display1();

        }
    }
Posted
Updated 1-Nov-13 6:20am
v2

Inheritance Means "Child Class Can Access The Functionlity Of It's Parent Class" But Reverse Is Not Alllowed (Parent Class Can Not Access The Functionlity Of It's Child Class)

The following Code Will Reise Error: Because you want to access child class function through parent class object, which is not allowed.

C#
Parent p1 = new Parent();
p1.Display1();


You can access only like this :

C#
Child c1 = new Child();
c1.Display1();
 
Share this answer
 
I think you are kind of looking at inheritance of Classes through the "wrong end of the telescope."

Yes, derived classes have immediate access to objects and methods in the Base Classes they derive from ... if ... the Base Class exposes those objects and methods by the use of modifiers, like 'public.

There are two other ways a derived Class can use its Base Class' objects and methods ... these ways are often not clearly understood by beginning programmers:

1. an instance of a derived Class can call methods, and set/get public variables/fields, of its Base Class even if an instance of the Base Class has never been created.

2. the special qualifier "base" followed by a dot and the name of publicly exposed variable/field, or method, in the Base Class: is another way the derived Class can use its Base Class' resources.

"base.SomePublicNameInBaseClass" cannot be "cascaded:" if:

Class3 derives from Class2 which derives from Class1: and, in Class3 you write: base.base.SomePublicNameInBaseClass that's an error.

base.SomePublicNameInBaseClass in Class3 will use the first SomePublicNameInBaseClass that it finds in its inheritance chain.

Here's a little code sample for you to try out and see if it helps you understand inheritance:
C#
public class Class1
{
    public Class1() { }

    private string mySecret = "shhhhh";

    public string notASecret = "no secret here";

    public void AllDerivedClassesCanUseMe()
    {
        Console.WriteLine("in Class1: all derived Classes can use me !");
    }

    private void NoDerivedClassCanUseMe()
    {
        Console.WriteLine(mySecret + " " + "derived Classes cannot use me !");
    }

    public void hello()
    {
        NoDerivedClassCanUseMe();
        Console.WriteLine("hello from Class1");
    }
}

public class Class2 : Class1
{
    public Class2() { }

    public void hello()
    {
        AllDerivedClassesCanUseMe();

        base.hello();

        Console.WriteLine("hello from Class2");
    }
}

public class Class3 : Class2
{
    public Class3() { }

    public void hello()
    {
        base.notASecret = "Class 3 knows a secret in Class1";

        AllDerivedClassesCanUseMe();

        base.hello();

        Console.WriteLine("hello from Class3");
    }
}

public class Class4
{
    public Class4() { }

    public void hello()
    {
        // make sure you understand why the next two lines cause error
        // AllDerivedClassesCanUseMe();
        // base.hello();

        Console.WriteLine("hello from Class4");
    }
}
I suggest you create a VS project, create a Class into which this code goes, and then in your code that launches the Project, do something like this:
//Class1 c1 = new Class1();
//Class2 c2 = new Class2();
Class3 c3 = new Class3();

c3.hello();
Try putting a breakpoint right where the code is going to create the instance of Class3, and then single-stepping through the code.

Run the project, and examine the output in the Console Window. Try un-commenting the different calls to create Class1, Class2, Class3, and see what happens. Change the order of creating the Classes and see what happens.

Finally, it would be possible for a Base Class to call methods or affect objects in its derived Classes if it had a valid reference to the instances of the derived Classes, and the derived Classes exposed their internal methods, or objects for public use . That might be unusual, but it is possible.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900