1. Trang chủ
  2. » Công Nghệ Thông Tin

Inheritance and Polymorphism - ABC programming lesson ppt

37 423 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Inheritance and Polymorphism - ABC programming lesson ppt
Trường học Unknown University
Chuyên ngành Computer Science / Programming
Thể loại Lecture Slides
Định dạng
Số trang 37
Dung lượng 587,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

"protected " Access Modifier Protected variables or methods are accessed only by:  The class in which they are declared  Derived class  Specified using the protected keyword... " new

Trang 1

Inheritance and Polymorphism

ABC programming lesson

Trang 2

Inheritance and Polymorphism

Welcome to the module Inheritance and

Polymorphism is a feature of OOP that allows the data members of a class to behave

differently based on their parameters and data types.

In this module, you will learn about:

 Implementing Inheritance

 Method Overriding

 Sealed Classes

 Polymorphism

Trang 3

#1 - Implementing Inheritance

Explain the concept of inheritance

Describe how to implement inheritance in C#.

State and explain the protected access modifier.

State and explain the base keyword.

State and explain the new keyword.

Describe how to inherit constructors.

Trang 4

Inheritance specifies an “is a kind of" relationship

 Inheritance is a class relationship

 New classes specialize existing classes

Musician

Violin Player

Violin Player

Trang 5

Purpose of Inheritance

Reusability

Reuse common methods and attributes among classes without recreating them.

 Use the same code in different

applications with little or no

changes.

Generalisation

Specialisation

Extension

Trang 7

Example of Inheritance

Animal

Cat

Trang 8

"protected " Access Modifier

Protected variables or methods are accessed only by:

 The class in which they are declared

 Derived class

Specified using the protected keyword.

Trang 9

" base " Keyword

Like “ super ” in Java

Access the variables and methods of the base class from the derived class.

Cannot use the base keyword for invoking the static methods of the base class.

Trang 10

Example of Using " base " Keyword

Trang 11

" new " Keyword

As a modifier to hide the methods or variables of the base class that are inherited in the derived class.

Trang 12

Example of " new " Keyword

Trang 13

Constructor Inheritance

In C#, cannot inherit constructors like inherit methods

The instance of the derived class will always first invoke the

constructor of the base class followed by the constructor of the derived class.

Can explicitly invoke the base class constructor by using the base

keyword in the derived class constructor declaration

Trang 14

Constructor Chaining

Parent

F1

F2

Trang 15

Constructor Chaining

Trang 16

Rules for Constructors (MUST be clear and remember)

A default constructor will be automatically generated by the compiler

if no constructor is specified within the class.

The default constructor is ALWAYS a no-arg constructor.

If there is a constructor defined in the class, the default constructor is

no longer used.

If you don’t explicitly call a base class constructor in a derived class constructor, the compiler attempts to silently insert a call to the base class’s default constructor before executing the code in the derived class constructor

Trang 17

Compiler-Generated Constructor Code

Trang 18

Can you see the Bugs?

Trang 19

How to fixed

Trang 20

Explicitly invoke the base class constructor

Trang 21

#2 - Method Overriding

Define method overriding.

Explain virtual and override keywords.

Trang 22

Method Overriding

Derived class to override or redefine the methods of the base class.

A method that is intended to be overridden is called a virtual method

The method implemented in the derived class from the base class is known as the Overridden Base Method.

Trang 23

Implementing Method Overriding

Using appropriate "virtual" and "override" Keywords

To override a particular method of the base class in the derived class:

 In the base class, declare the method using the “virtual” keyword.

 In the derived class, declare the inherited virtual method using the “override” keyword

Trang 24

Example of Implementing Method Overriding

Trang 25

Method Overriding Remarks

The overridden base method must have the same signature as the virtual method

Cannot override a non-virtual or static method.

Both the override method and the virtual method must have the same access level modifier.

Trang 26

Declaring new Methods

Trang 27

Calling the Base Class Method

Trang 28

#3 - Sealed Classes

Define sealed classes in C#

Describe the purpose of sealed classes.

Trang 29

Sealed Classes

Class that prevents inheritance.

 Like “final” class in Java

To declare, use sealed keyword before class keyword.

If a class tries to derive a sealed class, the C# compiler generates an error.

Trang 30

Purpose of Sealed Classes

Ensure Security

Protect from Copyright Problems

Prevent Inheritance

Trang 31

Using Sealed Class Guidelines

If overriding the methods of a class might result in in unexpected functioning of the class

To prevent any third party from modifying your class

Trang 32

#4 - Polymorphism

Define polymorphism in C#

Explain how to implement polymorphism

Describe compile and run-time polymorphism

Trang 33

Define polymorphism in C#

Polymorphism means existing in multiple forms

The ability of an entity to behave differently in different situations

Polymorphism allows methods to function differently based on the parameters and their data types

Implement polymorphism in C# through:

 Method overloading (Compile-time Polymorphism)

 Method overriding (Run-time Polymorphism)

Trang 34

Example of polymorphism in C#

A B C

Trang 35

Example of polymorphism in C#

Trang 36

Compile-time and Run-time Polymorphism

Compile-time Polymorphism

 Implemented through method overloading

 Executed at the compile-time

 Referred to as static polymorphism

Trang 37

That’s about all for today!

Ngày đăng: 09/07/2014, 13:21

TỪ KHÓA LIÊN QUAN