[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

parents not knowing their children

under current oop paradigms a base class has no idea of the classes that is derived from it which is like saying that a parent has a child but does not know that the child has blue eyes why?
1 Solution
A child class is an extension of  base class.

The base class is more general and doesn't incorporate the specific

Actually the logic is more like: the child has blue eyes, but the more
general class Lifeform (which the object instance 'humanChild' happens to be
an indirect member of) doesn't include the idea of having eyes.
This is not only OOP issue. Any program should be written by such way, that it's various parts don't know anything (or know minimum) about each other. In this case, when you change some part of the program, other parts remain unchanged.
About base class: suppose that base class knows something about derived classes. This means, it has some specific code for this. Changing of derived classes may require changing of the base class.
Class (parent) doesn't know anything about it's childs, it doesn't know whether it has childs.
This is the Liskov Substitution Principle.  A derived object should be easily substuted for a base object.  A class us used in terms of a public interface.  A client uses the class according to this interface.  A derived class must not change the results of using the interface from the client's perspective.  A Square and a Circle might be able to draw themselves, but the Circle can't require extra information about its radius, and the Square can't take control of the screen to optimize the drawing of itself based on its rectangular nature.  Requiring extra information and/or creating undocumented side-effects are not consistent with the base-class interface.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

I find thinking of a derived class as a "child class" unhelpful.
Prashant SabnekarAVPCommented:
Never think base class and derived class as, blood relations.
In programming we create base classes so that others can reuse it without any information to base class. And there is no point to know how many classes use it.
We can know how many instances has been created for our base class, by putting a static count variable in our base class.

Actually i am a bit busy now, but i will give you a very good answer soon
bye bye
>> doesn't include the idea of having eyes

I wouldn't agree to that. Baseclasses normally are designed to have a good part of common attributes of all derived classes. So the baseclass of HumanChild is Child or Human or PrimateChild, all of them have eyes.

>> Class (parent) doesn't know anything about it's childs

By defining virtual functions - especially with pure virtual functions - baseclasses can determine what kind of implementations a derived class has to provide.

>> it doesn't know whether it has childs.

That's true, but with pure virtual functions a baseclass or protected/private constructors a baseclass can prevent getting instantiated if not derived.

Regards, Alex

> A child class is an extension of  base class.
> The base class is more general and doesn't incorporate the specific elements.

A base class has a general functionality. for example when you are dealing with employees in an organization you would have classes like

1. mgmtEmployee has some added functionality that all employees don't have.
2. all employees are either temp or permanent. so you can think of permEmployee an tempEmployee as siblings with different propeties

and about your question "why?": so that a class can perform all functions independently without needing extraneous info.
In case, (this is occasional but not rare) that
1. you want to use a child class in parent class OR
2. have two classes dependent on each other
you can use this construct:

FILE: class1.h

#include "class2.h"

class class1{

FILE: class2.h

class class1;

class class2{
this question was answered several times over in the comments

I think  jhshukla  answered it most thoroughly and precisely

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now