Saturday, April 18, 2020

Oop Inheritance free essay sample

Sehubungan itu anda dikehendaki menyertakan contoh pewarisan yang bersesuaian dalam penerangan anda dan sertakan gambarajah sekiranya ia membantu penerangan anda. Seterusnya terangkan apakah kebaikan pewarisan dalam pengkodan aturcara kepada pelajar anda. INHERITANCE 1. 0 INTRODUCTION Inheritance is a relation given to the classes in a program. A class can acquire the other class members. Inherited members are available for a classs use as though they were defined in the same class. Inheritance occurs when one class is derived from another. The strength of inheritance is to reuse the existing class and modify the existing class to a new class. Reusing the existing code minimizes the re-occurrences of instructions in a program. This unit introduces you to the inheritance feature of OOP. Inheritance is the capability by which the objects of one class inherit the members of the objects in another class. Consider class1 and class2. Class1 is used to derive/create class2. We will write a custom essay sample on Oop Inheritance or any similar topic specifically for you Do Not WasteYour Time HIRE WRITER Only 13.90 / page Class1 is called the base class and class2 is called the derived class. To understand inheritance, consider the following example. Figure 1:Example for Inheritance In the Figure 1, the class Automobile can be classified into Car class Bus class In the example, Automobile is the base class. Car and Bus are called derived classes and belong to the base class Automobile. They share common characteristics and functions. For example, Automobiles are self-propelled vehicles that can move on the wheels. They are differentiated in few of their characteristics like number of seats, fuel type etc. 2. 0 ADVANTAGES OF INHERITANCE The advantages of inheritance are: * It allows the reusability of the base class function in the derived classes * The code and data are brought together in a class. It eliminates the duplication of code and promoting reuse. * Each derived class will only have the variables it needs. * Each derived class implements methods they way it needs to. 3. 0 TYPES OF INHERITANCE Inheritance enables to define a new class from an existing one. The existing class is the super or base class. The classes that are derived from the existing one are derived or subclasses. The relationship between the base and derived classes is referred as derivation or inheritance hierarchy. There are five ways of deriving a class as shown in Figure 2. Inheritance Single Inheritance Multiple Inheritances Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance Figure 2: Types of Inheritance 3. 0. 1 Single Inheritance Single inheritance is the type of inheritance where a derived class inherits only from one base class. This is shown in Figure 3. base_class derived _class Figure 3: Representation for Single Inheritance Consider the example of an Automobile. The base class is Automobile while the derived class is Car. Car inherits certain characteristics and functions only from the base class Automobile as shown in Figure 4. Automobile Car Figure 4: Example for Single Inheritance 3. 0. 2 Multiple Inheritance Multiple inheritance is the type of inheritance where a derived class inherits from many base classes. Consider the example of a child. A child inherits certain characteristics and functions of his/her father and mother as shown in Figure 5. Mother| | Father| | | | Child Figure 5: Example for Multiple Inheritance 3. 0. 3 Hierarchical Inheritance Hierarchical inheritance is the type of inheritance where many derived classes inherit from a single base class. Consider the example of an account. The savings, fixed deposit and the current account inherits the characteristics like account number, date of deposition of money in the account etc. nd functions like calculating the interest accordingly. This is represented in Figure 6. Account Savings Account| | Current Account| | | | Fixed Deposit Account Figure 6: Example for Hierarchical Inheritance 3. 0. 4 Multilevel Inheritance Multilevel inheritance is the type of inheritance, where a derived class inherits the members from the main base class. This derived class, in turn, acts as the base class for another class. Consider the example of a child. A child inherits from his/her father. The father inherits from his grandfather. This representation is shown in Figure 7. Grandfather Father Child Figure 7: Example for Multilevel Inheritance 3. 0. 5 Hybrid Inheritance Hybrid inheritance is the type of inheritance where multiple derived classes inherit members from a single base class. These derived classes further act as base classes for a single derived class. Consider the example of Apple. Green Apple and Golden Apple belong to the class of Apple. Hybrid Apple is obtained by the combination of Green and Golden apple. This representation is shown in Figure 8. Apple Hybrid Apple Green Apple Golden Apple Apple Hybrid Apple Green Apple Golden Apple Figure 8: Example for Hybrid Inheritance 4. 0 CONCLUSION Types of Inheritance| Description| Single| One derived class inherits members fromone base class| Multiple| One derived class inherits members frommany base classes| Hierarchical| Many derived classes inherit membersfrom one base class| Multilevel| One derived class inherits the membersfrom the main base class. This derivedclass, in turn, acts as the base class foranother class. | Hybrid| One derived class inherits members from many base classes and these base classes inherit members from one base class.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.