Search code examples
c#.netabstract-classabstraction

Can i access a concrete method of an abstract class in direct child class?


Is there a way we can access the concrete method's of an abstract class in the direct child class as below

  abstract class ParameterBase
  {           
        public void test()
        { 
            string name = "testname";
            console.writeline(name);
        }          
  }

  public  class Parameter1 : ParameterBase 
  {
       //I Need to call(access) the Test() Method here i.e print "testname" in the  console         
  }

Now i know that we can create a instance of the child class with type as ParameterBase and access the test() method that is there in ParameterBase() as below

   ParameterBase PB = new Parameter1();
   PB.test();

Solution

  • You have to maintain the accessibility level while inheriting a class. You can do this :

    abstract class ParameterBase
    {
        public void test()
        {
            string name = "testname";
            Console.WriteLine(name);
        }
    }
    
    class Parameter1 : ParameterBase
    {
        void getvalue()
        {
            Parameter1 pb = new Parameter1();
            pb.test();
        }        
    }