If found the method is executed using the instance

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: of existing code. In a hierarchy of classes, a sub-class inherits all of the instance variables, instance methods, and class methods of its super-class/parent-class. The sub-class can also have its own instance variables, which must have different names than the variable names in its ancestor classes. Finally, the sub-class can define both new methods and methods that already exist in an ancestor class. When a sub-class has a method whose name or protocol is the same as an ancestor class, the sub-class method hides that of the ancestor class. Access to such a hidden method is normally provided by prefixing the message with the pseudo-variable "super". This causes the method search to begin in the super-class rather than locally. Inheritance need not get limited to one parent-class. The child class can inherit from multiple parent classes. This is called multiple inheritance. Figure 12.14 describes persons by an inheritance hierarchy of classes (representing behaviors). The person class has male and female as its sub-classes. The male class has person as its super-class and boy and man as its sub-classes. The instances Ram, Shyam, Mohan, Seeta, Geeta each have a unique base class. Figure 12.15 illustrates the concepts of object, class and inheritance with the help of an example. In the exampl object 'a' is an instance of class 'A' and object 'b' is an instance of class 'B'. Both A and B define behavior t operations shared by their instances, and have instance variables that cause a private copy to be created for each instance for the class or sub-class. The instance 'a' of class 'A' has a copy of A's instance variables and a pointer to its base class. The instance 'b' of class 'B' has a copy of the instance variables of both class 'B' and its super class 'A' and a pointer to the base class of 'B'. The representation of class 'B' has a pointer to its super-class 'A', while class 'A' has no super-class pointer, since it has no superclass. When object 'b' receives a message to execute a method, it looks first in the methods of class 'B'. If found, the method is exec...
View Full Document

Ask a homework question - tutors are online