Inheritance

Monday, January 14, 2008

Inheritance

Inheritance


Another important concept of object-oriented programming is inheritance. Inheritance allows a class to have the same behavior as another class and extend or tailor that behavior to provide special action for specific needs.

Let's use the following application as an example. Both Graduate class and Undergraduate class have similar bahavior such as managing a name, an address, a major, and a GPA. Rather than put this bahavior in both of these classes, the bahavior is placed in a new class called Student. Both Graduate and Undergraduate become subclass of the Student class, and both inherit the Student behavior.

Both Graduate and Undergraduate classes can then add additional behavior that is unique to them. For example, Graduate can be either Master's program or phD program. On the other hand, Undergraduate class might want to keep track of either the student is Freshman, Sophmore, Junior or Senior.

Classes that inherit from a class are called subclasses. The class a subclass inherits from are called superclass. In the example, Student is a superclass for Graduate and Undergraduate. Graduate and Undergraduate are subclasses of Student.

0 comments:

 

Tamil Video Songs | Make Money Online