0

I've been working with PHP for a while now I am trying to explore the world of c#.I was trying to understand the abstract class implementation in c sharp.I've found some fluctuation in behavior in both languages.That's why I am posting it here so that I can get a better insight of abstract class implementation in C#.

Both PHP and C# extends abstract class.That means all the class that extends from abstract class must implement the abstract methods as well as inherit non-abstract public methods from abstract class.

The problem arises while accessing the property of child class from a method of the abstract class. In PHP getHairColor() can access the public hair property. But in C# getHairColor() can't access the public hair property of the class that extends from abstract class.

I think as the Male class extends Person class, it also inherits the getHairColor() method from the parent class.So, it should be able to access the hair property. Here PHP implementation makes more sense than C#. Can I get a proper explanation for different behavior for a similar concept in two different languages?

PHP:

abstract class Person{
    abstract public function setHairColor($color);

    public function getHairColor(){
        return $this->hair;  // $this->hair is accessible from here
    }
}

class Male extends Person{
    public $hair;

    public function setHairColor($color){
        $this->hair = $color;
    }

}

$male1 = new Male();
$male1->setHairColor("black");
echo $male1->getHairColor(); // access hair property

C#:

abstract class Person
{
    public abstract void setHairColor(string hair);

    public string getHairColor()
    {
        return this.hair; // this.hair is not accessible from here
    }

}

class Male : Person
{
    public string hair;

    public override void setHairColor(string hair)
    {
        this.hair = hair;
    }
}

Male male1 = new Male();
male1.setHairColor("black");
Console.WriteLine(male1.getHairColor()); // can not access hair property 
4
  • No, properties that you define in children are not accessible from the parent. If all Persons have hair, why don't you define hair in Person? Commented Jul 9, 2017 at 10:48
  • 1
    What will happen in PHP if you extend Person and don't define hair? Commented Jul 9, 2017 at 10:50
  • But it is extending from abstract class.So getHairColor is an inherited method for child class.That's why i think it should access hair property.So,which language is more logical regarding the implementation of abstract class concept. Commented Jul 9, 2017 at 10:51
  • Why would your (abstract) base class know anything about its children? If you find that logical, you're saying that if I implement Dog : Animal, and a Dog can bark, all of a sudden, I can make all Animal.Bark(), just because I defined Bark(); in one of its descendants. Commented Jul 9, 2017 at 11:40

1 Answer 1

1

C# has static typing. That's why you can't access a field or property that is declared in a subclass. You have to declare Hair as an abstract property in Person and implement it in the subclass:

    abstract class Person
    {
        public abstract void SetHairColor(string hair);

        public abstract string Hair { get; }

        public string getHairColor()
        {
            return this.Hair; // now it is accessible
        }

    }

    class Male : Person
    {
        private string _Hair;
        public override string Hair { get { return _Hair; } }

        public override void SetHairColor(string hair)
        {
            this._Hair = hair;
        }
    }

You can also implement SetHairColor as setter of the property Hair:

    abstract class Person
    {
        public abstract string Hair { get; set; }

        public string getHairColor()
        {
            return this.Hair; // now it is accessible
        }

    }

    class Male : Person
    {
        private string _Hair;
        public override string Hair 
        { 
            get { return _Hair; } 
            set { _Hair = value }
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for you reply.You mentioned the term static typing. But can you explain what does static typing have to do for this behavior ?
For a general explanation of the term static typing, you might look into this: csharp.2000things.com/tag/static-typing , or this: alorelang.org/doc/typeoverview.html
C# accepts only those references that can be resolved at compile-time. If the field hair is not declared in the abstract class, how should the compiler know that there's a field hairin all possible subclasses? You might somewhere define a subclass BaldHead that doesn't have a field hair. But declaring an abstract property Hair in Person ensures that all sublasses implement such a property. In languages with dynamic typing, this would be checked at runtime, and if you tried to access the hair of a BaldHead, you'd get a runtime-error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.