user767638
user767638

Reputation:

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();

Upvotes: 0

Views: 8939

Answers (3)

Mohamed FakhrElDin
Mohamed FakhrElDin

Reputation: 1

You can do this

class Program
{
    static void Main(string[] args)
    {
        Parameter1 parameter1 = new Parameter1();
        parameter1.te();
        Console.ReadLine();
    }
}

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

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

Upvotes: 0

isree
isree

Reputation: 53

Please look into this:-

class Program
{
    static void Main(string[] args)
    {
        Test2 t = new Test2();
        t.display();
        t.absDsisplay();
    }
}

abstract class Test1
{
    public void display()
    {
        Console.WriteLine("display");
    }
    public abstract void absDsisplay();

}
class Test2 : Test1
{

    void GetValu()
    {


    }

    public override void absDsisplay()
    {
        Console.WriteLine("absDisplay");
    }
}

Upvotes: 0

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

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();
    }        
}  

Upvotes: 3

Related Questions