[Solved] Parent and Child in OOP


First one is correct, second is not.

By definition, a Child will inherit all properties and methods of a Parent; however, Parent will not have all properties and/or methods of a Child, so the second statement wouldn’t make sense:

class Parent 
{
    public int ParentId { get; set; }

    public void Eat { ... }
}

class Child : Parent
{
    public int ChildId { get; set; }

    public void Play { ... }
}

Parent child = new Child();
child.Eat(); // this makes sense since this is common functionality

Child parent = new Parent();
parent.Play() // this does not make sense since a Parent doesn't know hot to play

solved Parent and Child in OOP