Deriving
a subclass
The derived new class is
called a subclass. It inherits the attributes and methods of its base class. It
can also have its own attributes and methods in addition to the ones that are
inherited from the parent class. The keyword ‘extends’ is used to derive a
subclass in JAVA.
Public
vs. Private or Protected
Public variables and
methods are inherited from the parent class. Children classes do not inherit
private attributes and methods. Protected data can be accessed by any class in
the same package. Protected type of access provides an intermediate between
public and private access type.
A child class can be
derived privately from its parent class. For
example in the public class Car, Engine is privately derived from the base class
Car.
class Car: private Engine { // Car has-a Engine
public:
Car() : Engine(8) { } // Initializes this Car with 8 cylinders
using Engine::start; // Start this Car by starting its Engine
};