Document 9653961

Download Report

Transcript Document 9653961

Mata kuliah : M0874 – Programming II
Tahun
: 2010
Object-Oriented Programming Inheritance
Session 06
Outline Materi
•Introduction
•Base Classes and Derived Classes
•Protected and internal Members
•Relationship between Base Classes and Derived
Classes
Bina Nusantara University
3
Introduction
•One of object-oriented features are inheritance.
•Inheritance is a form of software reuse in which a new class is created by
absorbing an existing class’s members and enhancing them with new or
modified capabilities.
•Inheritance lets programmers save time during application development
by reusing proven and debugging high-quality software.
•Inheritance also increases the likelihood that a system will be implemented
effectively.
4
Base Classes and Derived Classes
•The existing class from which a new class inherits members is called base class, and the
new class is the derived class.
•Each derived class can become the base class for future derived classes.
•A derived class normally adds its own fields and methods. Therefore, its more specific
than its base class and represents a more specialized group of objects.
•Typically, the derived class exhibits the behaviors of its base class and additional
behaviors that are specific to itself.
5
Base Classes and Derived Classes
•A parent class can inherit its behavior and state to children classes.
•This concept was developed to manage generalization and specialization
in OOP and is represented by a is-a relationship.
•The following OO terms are commonly used names given to parent and
child classes in OOP:
•Superclass: Parent class.
•Subclass: Child class.
•Base class: Parent class.
•Derived class: Child class.
6
Base Classes and Derived Classes
Base Class
Derived Classes
Student
GraduateStudent, UndergraduateStudent
Shape
Circle, Triangle, Rectangle
Loan
CarLoan, HomeImprovementLoan, MortgageLoan
Employee
Faculty, Staff, HourlyWorker, CommissionWorker
BankAccount
CheckingAccount, SavingsAccount
Bina Nusantara University
7
Protected and internal Members
•Access modifier:
•Public: accessible wherever the application has a
reference to an object f that class or one of its
derived classes.
•Private: accessible only within the class itself.
•Protected: intermediate level of access between
public and private. A base class’s protected
members can be accessed by members of that base
class and by members of its derived classes.
Bina Nusantara University
8
Relationship between Base Classes and Derived
Classes
Example a base class and derive another class from
this base class
Bina Nusantara University
9
Relationship between Base Classes and Derived
Classes
Bina Nusantara University
10
References
•http://www.codeproject.com/KB/cs/csharpintro01.a
spx
•http://books.google.co.id/books?id=8R451rZJ5o0C
&printsec=frontcover&dq=c%23&source=gbs_simila
rbooks_s&cad=1#v=onepage&q=&f=false
Bina Nusantara University
11