Inheritance
Once a class is defined, it may be used as a model for another class by
using the C++ syntax class Child : public Parent
or Java syntax class Child extends Parent.
The new Child class
inherits all of the attributes, and methods of the Parent class.
Additional attributes and methods may then be added to the Childclass,
without effecting the Parent class.
The Childclass is derived from the Parent.
Here is an easy to understand example, using three classes.
// C++ implementation
class Grandpa class Mom class Dad : public Grandpa
{ { {
protected: protected: protected:
int big_ears; float eyes; char mouth;
. . .
. . .


} } }
// Java implementation
public class Grandpa public class Mom public class Dad extends Grandpa
{ { {
protected int big_ears; protected float eyes; protected char mouth;
.... .... ....
} } }
Notice that Dad is derived from Grandpa . We will now declare
one more class that will bring these together.
// C++ allows multiple inheritance
class Me : public Mom, public Dad
{
private:
unsigned int freckles;
}
|
  
|
The class me will end up with Mom's eyes, Dad's mouth, and Grandpa's
big_ears, since Dad was derived from Grandpa. In this example, freckles
is an attribute that only exists in the class me. |
// Java doesn't support multiple inheritance
public class Me extends Dad
{ // notice that class Me inherits from Dad ONLY
private int freckles;
private float eyes; // not from Mom
...
} |
This example only dealt with data, however the situation would
occur with any methods (functions) that were contained in the base classes.
Question: Why PROTECTED is used ?
|
Back to Object Oriented Concepts