Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have method DisplayData which is inherited and i am succefully able to call this method in inherited class. But unfortunately I am unable to call this method in main class i have created the object of for inherited class and tried accessing the DisplayData method its not shown. I am aware that when you inherit any class in another class you will be able to access all the methods and member fields but i am unable to do.. Please clarify me. My requirement is can i access the DisplayData() method in main class
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class student
{
    protected int _stdID;
    protected string _stringName;


    Protected void DisplayData()
    {

        Console.WriteLine("Name of the student {0} and id {1}", _stringName, _stdID);
    }

}
    class Csstudent : student
    {

        private int _sub1, _sub2;

        public Csstudent(int id, string name, int sub1, int sub2)
        {
            _stdID = id;
            _stringName = name;
            _sub1 = sub1;
            _sub2 = sub2;

        }


        public void StudentDetails()
        {
           //DisplayData();
            Console.WriteLine("Subject marks1 {0} and subject marks2 {1}",_sub1,_sub2);
        }
    }




    class Program 
{
    static void Main(string[] args)
    {


        Csstudent cs = new Csstudent(101, "Sha", 56, 96);

        cs.StudentDetails();
          

        
    }
}
Posted
Updated 28-May-14 2:25am
v2
Comments
ArunRajendra 28-May-14 8:23am    
Yes since the method is public you should be able to call it from main using inherited class object. If you are getting error post the error description.
ShaHam11 28-May-14 8:25am    
sorry the DisplayData() method suppose to be protected
Sayan Bera 28-May-14 8:39am    
will this work?

class Program: student
{
static void Main(string[] args)
{
Csstudent cs = new Csstudent(101, "Sha", 56, 96);
cs.StudentDetails();

Program pp = new Program();
pp.DisplayData();

Console.ReadLine();
}
}

1 solution

Quote:
My requirement is can i access the DisplayData() method in main class

You actually can, the following code should (and it does, on my system) compile and run fine.
C#
static void Main(string[] args)
    {
 

        Csstudent cs = new Csstudent(101, "Sha", 56, 96);
 
        cs.StudentDetails();
        cs.DisplayData();
 
        
    }
 
Share this answer
 
Comments
ShaHam11 28-May-14 8:30am    
@Cpallini this works, I just edited my code what if the method was protected I am unable to access I remember as per access spcifter protected you can access in derived class also.. so i created a object for derived class in main class and tried accessing it .. its not showing up in the intellisense
Sayan Bera 28-May-14 8:42am    
You can access Protected member of a class(student) only in the class(Csstudent) which derives it, Not any other class(Program).

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