Oop Inheritance

Only available on StudyMode
  • Download(s) : 114
  • Published : May 27, 2013
Open Document
Text Preview
MTS 6014
ADVANCE PROGRAMMING

GROUP PROJECT

PROGRAMMING
INHERITANCE

PREPARED FOR :
DR. NOR HASBIAH BINTI UBAIDULLAH

PREPARED BY:
MARAN CHANTHIRAN M20121000841
MUSTAKIM BIN BAHARUDIN M20121000839
SHARIMAN HAFIZAN BIN SHAMSUDIN M20112001355

Tugasan 1
Sebagai seorang guru yang mengajar pengaturcaraan berorientasi objek, anda perlu menerangkan salah satu konsep penting dalam OOP iaitu pewarisan kepada pelajar dalam kelas anda. Penerangan anda perlu jelas agar ia mudah difahami oleh pelajar. 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 class's 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. 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...
tracking img