inheritance

inheritance - Inheritance - to derive a new class from an...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Inheritance -- to derive a new class from an existing class. In stead of repeating similar data/method definitions in several classes, we can define one class with the common instance data/methods, and let other classes inherit these data/method definitions. For instance, if we are trying to define Cat class, Dog class, and Raccoon class and know all of them will need instance data tail and legs, we can define them in another class called “Animal” and let Cat, Dog, and Raccoon classes inherit from it. Animal int tail; int legs; Cat Dog Raccoon Here the Animal class is called “ parent class ”, “ super class ”, or “ base class The classes Cat, Dog, and Raccoon are called “ child class ” or “ sub class ”. The child classes will inherit the methods and data defined for the parent class. However, child classes do not inherit private variables and methods. They can inherit public variables and methods. Making instance data (variables) public violates Encapsulation. There is another visibility modifier called protected” . Protected variables and methods can be inherited by child classes, but not as accessible as public. If you plan to have a class inherited by other classes, its data should be declared as “protected”.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Visibility Modifiers public -- inherited, can be accessed from anywhere (we use + for UML class diagrams) private – not inherited, can be accessed only within its class (we use – for UML class diagrams) protected – inherited, can be accessed by any class in the same package
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

inheritance - Inheritance - to derive a new class from an...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online