Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • 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?
0
GrooverMD
Asked:
GrooverMD
1 Solution
 
MysidiaCommented:
A child class is an extension of  base class.

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

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.
0
 
AlexFMCommented:
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.
0
 
teratomaCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rstaveleyCommented:
I find thinking of a derived class as a "child class" unhelpful.
0
 
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
Prashant
0
 
itsmeandnobodyelseCommented:
>> 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



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

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

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{
};
0
 
MysidiaCommented:
this question was answered several times over in the comments

I think  jhshukla  answered it most thoroughly and precisely
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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